刚从cv2开始,我想要的是在某个坐标窗口中为该对象提供种子,并让他连接可能在初始坐标框之外但与之接触的所有像素。
我从小型测试开始,以了解连接的componnet:
im=cv2.imread('test.png', 0)
ret, thresh = cv2.threshold(im, 254, 255, cv2.THRESH_BINARY)
output = cv2.connectedComponentsWithStats(thresh, 4, cv2.CV_32S)
然后
im=cv2.imread('test.png', 0)
ret, thresh = cv2.threshold(im, 254, 255, cv2.THRESH_BINARY)
thresh = cv2.bitwise_not(thresh)
output = cv2.connectedComponents(thresh, 4, cv2.CV_32S)
这两个outpute数组,到目前为止到目前为止还不错,然后我想查看引用docs https://docs.opencv.org/3.0-beta/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#connectedcomponents
connectedComponentsWithStats(InputArray image, OutputArray labels, OutputArray stats, OutputArray centroids, int connectivity=8, int ltype=CV_32S)
和labels – destination labeled image
的实际输出图像,因此我更改了上面共享的小代码中的最后一行:output = cv2.connectedComponents(thresh,"out_test.png" ,4, cv2.CV_32S)
它给了我在问题中共享的错误。我还尝试了:
cv2.imwrite(dest_dir+"out_test.png", output)
并得到此错误:
TypeError: img is not a numerical tuple
我实际上如何可视化输出,因为我不想计算blob(对象),它们的大小或其他任何东西,我只希望它们从我给的原始感兴趣区域中增长。
最佳答案
Help on built-in function connectedComponents:
connectedComponents(...)
connectedComponents(image[, labels[, connectivity[, ltype]]]) -> retval, labels
. @overload
.
. @param image the 8-bit single-channel image to be labeled
. @param labels destination labeled image
. @param connectivity 8 or 4 for 8-way or 4-way connectivity respectively
. @param ltype output image label type. Currently CV_32S and CV_16U are supported.
import cv2
fname = "test.png"
img=cv2.imread(fname, 0)
ret, thresh = cv2.threshold(img, 254, 255, cv2.THRESH_BINARY)
thresh = cv2.bitwise_not(thresh)
nums, labels = cv2.connectedComponents(thresh, None, 4, cv2.CV_32S)
dst = cv2.convertScaleAbs(255.0*labels/nums)
cv2.imwrite("dst.png", dst)