這是一個完美的示例影像,其中包括一系列黑色物體。

這個代碼假設找到每一個黑色植物
import cv2 as cv
import numpy as np
img = cv.imread("12.jpg")
tresh_min= 200
tresh_max=255
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
blurred = cv.GaussianBlur(gray, (5, 5), 0)
_, threshold = cv.threshold(blurred, tresh_min, tresh_max, 0)
(contours, _)= cv.findContours(threshold, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
print(f'Number of countours: {len(contours)}')
mask = np.ones(img.shape[:2], dtype="uint8") * 255
# Draw the contours on the mask
cv.drawContours(mask, contours=contours, contourIdx=-1, color=(0, 255, 255), thickness=2)
但結果令人失望,因為這

其中包括 118 個輪廓。請注意,我需要找到并獲取 14 個物件。當輪廓實際上不正確時如何切割每株植物。或者至少將非常接近的輪廓連接到每個較大的輪廓,以分別保存物件?謝謝
uj5u.com熱心網友回復:
我們可以使用dilate代替GaussianBlur,使用RETR_EXTERNAL代替RETR_TREE,并且只保留大輪廓。
反轉
threshold:_, threshold = cv.threshold(gray, tresh_min, tresh_max, cv.THRESH_BINARY_INV)用柱核膨脹(假設植物又高又窄):
dilate_threshold = cv.dilate(threshold, np.ones((15, 1), np.uint8))回圈遍歷輪廓串列并僅保留面積大于 1000 的輪廓。
代碼示例:
import cv2 as cv
import numpy as np
img = cv.imread("12.jpg")
tresh_min= 200
tresh_max=255
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
#blurred = cv.GaussianBlur(gray, (5, 5), 0)
_, threshold = cv.threshold(gray, tresh_min, tresh_max, cv.THRESH_BINARY_INV)
dilate_threshold = cv.dilate(threshold, np.ones((15, 1), np.uint8))
(contours, _)= cv.findContours(dilate_threshold, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) # Use RETR_EXTERNAL instead of RETR_TREE
print(f'Number of countours: {len(contours)}')
mask = np.ones(img.shape[:2], dtype="uint8") * 255
# Draw the contours on the mask
large_contours = []
for c in contours:
area_tresh = 1000
area = cv.contourArea(c)
if area > area_tresh:
cv.drawContours(mask, [c], contourIdx=-1, color=(0, 255, 255), thickness=1)
large_contours.append(c) # Append to list of "large contours".
print(f'Number of large countours: {len(large_contours)}')
# Show output for testing
cv.imshow('threshold', threshold)
cv.imshow('dilate_threshold', dilate_threshold)
cv.imshow('mask', mask)
cv.waitKey()
cv.destroyAllWindows()
輸出mask:

uj5u.com熱心網友回復:
這是如何使用連接組件解決此問題的想法。解決方案是用 C 撰寫的,但用 python 撰寫應該沒什么大不了的。
auto original = cv::imread("image.jpg");
cv::Mat gray;
cv::cvtColor(original, gray, cv::COLOR_BGR2GRAY);
cv::imshow("Gray", gray);
cv::Mat thresh, dilatedImage, erodedImage;
cv::threshold(gray, thresh, 253, 255, cv::THRESH_BINARY_INV);
cv::imshow("Threshold", thresh);
auto kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
cv::dilate(thresh, dilatedImage, kernel);
cv::imshow("Dilated Image", dilatedImage);
cv::Mat labels;
auto numberOfComponentes = cv::connectedComponents(dilatedImage, labels);
std::cout << "Number of components: " << numberOfComponentes << std::endl;
std::cout << "Number of blobs: " << numberOfComponentes - 1 << std::endl;
// Show all components separatly
for (int i = 1; i < numberOfComponentes; i)
cv::imshow(std::to_string(i), labels == i);
cv::waitKey(0);
cv::destroyAllWindows();
編輯:
使用此解決方案檢測到的所有組件。有很多視窗,因為每個組件都是單獨顯示的。

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/447025.html
標籤:python-3.x 麻木的 opencv 图像处理 opencv轮廓
上一篇:OpenCvDnnSuperResImpl(C )例外
下一篇:找不到opencv函式的來源
