TypeError: labels is not a numpy array, neither a scalar

TypeError: labels is not a numpy array, neither a scalar

刚从 cv2 开始,我想要的是在一些 window 坐标中给对象一个种子,并让他连接所有可能在初始坐标框之外但接触的像素用它。 我从小测试开始,以了解连接的组件:

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)

这两个输出数组,到目前为止还不错,然后我想查看参考文档 https://docs.opencv.org/3.0-beta/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#connectedcomponentsconnectedComponentsWithStats(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)

它给了我 question.i 中共享的错误也试过:

cv2.imwrite(dest_dir+"out_test.png", output)

并收到此错误:

TypeError: img is not a numerical tuple

我如何实际可视化输出,因为我不想计算斑点(对象)、它们的大小或任何其他我只想让它们从我给出的原始感兴趣区域增长的东西。

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)

如果你想让白色斑点长大,你可以使用 Morphological Transformations

在使用之前了解函数的作用

cv2.connectedComponents

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

@ausk 的回答应该对你有用

import cv2

在终端中打开 python 然后

例子

help(cv2.connectedComponents)

希望对您有所帮助

import cv2
import numpy as np
from matplotlib import pyplot as plt

image = cv2.imread("image.jpg")
grayscaleImage = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresholdedImage = np.zeros((image.shape[0],image.shape[1]),np.uint8)
thresholdedImage[grayscaleImage<250]=[255]

interestedObjects, interestedObjectContours, interestedObjectsHierarchy = cv2.findContours(thresholdedImage,cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)


for i, l in enumerate(interestedObjectContours):
    rect = cv2.minAreaRect(interestedObjectContours[i])
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    box[box < 0] = 0
    cv2.drawContours(image, [box], 0, (0, 255, 0), 2)


plt.subplot(111), plt.imshow(cv2.cvtColor(image,cv2.COLOR_BGR2RGB))
plt.title('Your objects detected image'), plt.xticks([]), plt.yticks([])
plt.show()