def test_mat_construct(self): data = np.random.random([10, 10, 3]) #print(np.ndarray.__dictoffset__) # 0 #print(cv.Mat.__dictoffset__) # 88 (> 0) #print(cv.Mat) # <class cv2.Mat> #print(cv.Mat.__base__) # <class 'numpy.ndarray'> mat_data0 = cv.Mat(data) assert isinstance(mat_data0, cv.Mat) assert isinstance(mat_data0, np.ndarray) self.assertEqual(mat_data0.wrap_channels, False) res0 = cv.utils.dumpInputArray(mat_data0) self.assertEqual(res0, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=300 dims(-1)=3 size(-1)=[10 10 3] type(-1)=CV_64FC1") mat_data1 = cv.Mat(data, wrap_channels=True) assert isinstance(mat_data1, cv.Mat) assert isinstance(mat_data1, np.ndarray) self.assertEqual(mat_data1.wrap_channels, True) res1 = cv.utils.dumpInputArray(mat_data1) self.assertEqual(res1, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=100 dims(-1)=2 size(-1)=10x10 type(-1)=CV_64FC3") mat_data2 = cv.Mat(mat_data1) assert isinstance(mat_data2, cv.Mat) assert isinstance(mat_data2, np.ndarray) self.assertEqual(mat_data2.wrap_channels, True) # fail if __array_finalize__ doesn't work res2 = cv.utils.dumpInputArray(mat_data2) self.assertEqual(res2, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=100 dims(-1)=2 size(-1)=10x10 type(-1)=CV_64FC3")
def qimage2CvMat(inImage, inCloneImageData=True): if inImage.format() == QImage.Format_ARGB32 or inImage.format( ) == QImage.Format_ARGB32_Premultiplied: m = cv2.Mat(inImage.height(), inImage.width(), cv2.CV_8UC4, inImage.bits(), inImage.bytesPerLine()) return (m.clone() if inCloneImageData else m) elif inImage.format() == QImage.Format_RGB32 or inImage.format( ) == QImage.Format_RGB888: if inCloneImageData == False: qWarning( "QImageToCvMat() - Conversion requires cloning because we use a temporary QImage" ) swapped = inImage if inImage.format() == QImage.Format_RGB32: swapped = swapped.convertToFormat(QImage.Format_RGB888) swapped = swapped.rgbSwapped() return cv2.Mat(swapped.height, swapped.width(), cv2.CV_8UC3, swapped.bits(), swapped.bytePerLine()).clone() elif inImage.format() == QImage.Format_Indexed8: mat = cv2.Mat(inImage.height(), inImage.width(), cv2.CV_8UCl, inImage.bits(), inImage.bytePerLine()) return (mat.clone() if inCloneImageData else mat) else: qWarning("QImageToCvMat() - QImage format not handled in switch:" + inImage.format()) return cv2.Mat()
def test_comparison(self): # Undefined behavior, do NOT use that. # Behavior may be changed in the future data = np.ones((10, 10, 3)) mat_wrapped = cv.Mat(data, wrap_channels=True) mat_simple = cv.Mat(data) np.testing.assert_equal(mat_wrapped, mat_simple) # ???: wrap_channels is not checked for now np.testing.assert_equal(data, mat_simple) np.testing.assert_equal(data, mat_wrapped)
def test_mat_wrap_channels_fail(self): data = np.random.random([2, 3, 4, 520]) mat_data0 = cv.Mat(data) assert isinstance(mat_data0, cv.Mat) assert isinstance(mat_data0, np.ndarray) self.assertEqual(mat_data0.wrap_channels, False) res0 = cv.utils.dumpInputArray(mat_data0) self.assertEqual(res0, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=12480 dims(-1)=4 size(-1)=[2 3 4 520] type(-1)=CV_64FC1") with self.assertRaises(cv.error): mat_data1 = cv.Mat(data, wrap_channels=True) # argument unable to wrap channels, too high (520 > CV_CN_MAX=512) res1 = cv.utils.dumpInputArray(mat_data1) print(mat_data1.__dict__) print(res1)
def read32FC1Mat(data, scale): if len(data) == 0: return [] bias16U = cv2.imdecode(asMat(data), cv2.IMREAD_ANYDEPTH) bias32F = bias16U.astype(np.float32) mat32F = bias32F mat32F[:, :] -= Encode32FBias if scale == 0: return cv2.Mat() else: return mat32F / scale
def test_mat_construct_4d(self): data = np.random.random([5, 10, 10, 3]) mat_data0 = cv.Mat(data) assert isinstance(mat_data0, cv.Mat) assert isinstance(mat_data0, np.ndarray) self.assertEqual(mat_data0.wrap_channels, False) res0 = cv.utils.dumpInputArray(mat_data0) self.assertEqual(res0, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=1500 dims(-1)=4 size(-1)=[5 10 10 3] type(-1)=CV_64FC1") mat_data1 = cv.Mat(data, wrap_channels=True) assert isinstance(mat_data1, cv.Mat) assert isinstance(mat_data1, np.ndarray) self.assertEqual(mat_data1.wrap_channels, True) res1 = cv.utils.dumpInputArray(mat_data1) self.assertEqual(res1, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=500 dims(-1)=3 size(-1)=[5 10 10] type(-1)=CV_64FC3") mat_data2 = cv.Mat(mat_data1) assert isinstance(mat_data2, cv.Mat) assert isinstance(mat_data2, np.ndarray) self.assertEqual(mat_data2.wrap_channels, True) # __array_finalize__ doesn't work res2 = cv.utils.dumpInputArray(mat_data2) self.assertEqual(res2, "InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=500 dims(-1)=3 size(-1)=[5 10 10] type(-1)=CV_64FC3")
def displayImg(): while (not stopFlag.isSet()): try: print('before displayImg read ') rawImg = process.stdout.read(100) print('-----stdout byts------: ', rawImg) process.stdout.flush() return if len(rawImg) != WIDTH * HEIGHT * 3: continue img = np.fromstring(rawImg, dtype='uint8') img = img.reshape((HEIGHT, WIDTH, 3)) mat = cv2.Mat(img) cv2.imshow('drone', mat) cv2.waitKey(0.01) except Exception as e: print("displayImg error", e)
def fit(proj_skeleton, mask): return cv2.Mat()
def test_ufuncs(self): data = np.arange(10) mat_data = cv.Mat(data) mat_data2 = 2 * mat_data self.assertEqual(type(mat_data2), cv.Mat) np.testing.assert_equal(2 * data, 2 * mat_data)