gpt4 book ai didi

OpenCV:如何删除不需要的 Blob 或如何将想要的部分复制到空图像中?

转载 作者:行者123 更新时间:2023-12-02 17:08:33 30 4
gpt4 key购买 nike

从下图中,我如何找到结果图像?

enter image description here

此处显示的图像是阈值图像。我尝试过使用形态运算符,但它们甚至删除了我想要的 blob。我该如何解决这个问题?
有什么提示吗?

以下是我有兴趣获取/查找的结果图像:

enter image description here

import cv2
diff = cv2.imread('Image.png',0)
ret, thresh = cv2.threshold(diff, 12.5, 255, cv2.THRESH_BINARY)
thresh = cv2.dilate(thresh, None, iterations = 1)
cv2.imshow('img', thresh) # This is the first picture I have shown
cv2.waitKey(0)

最佳答案

你已经到了那里,你现在需要做的就是找到 Blob ,添加一些轮廓并找到最大的。简单的!以下是 C++ 中的代码,请自行决定如何将其转换为 Python:

cv::Mat mat = imread("g0cVU.png");
Mat origImage = mat;
Mat canny_output = mat;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
cv::Mat greyMat, colorMat;
cv::cvtColor(mat, greyMat, CV_BGR2GRAY);
int thresh = 100;
RNG rng(12345);
///// Detect edges using canny
Canny(greyMat, canny_output, thresh, thresh * 2, 3);
/// Find contours
findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
int largest_area = 0;
int largest_contour_index = 0;
Rect bounding_rect;
/// Draw contours
Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);
for (int i = 0; i< contours.size(); i++)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());
double a=contourArea( contours[i],false); // Find the area of contour
if(a>largest_area){
largest_area=a;
largest_contour_index=i; //Store the index of largest contour
bounding_rect=boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
}
}
rectangle(origImage, bounding_rect, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)),2);
/// Show in a window
namedWindow("Contours", CV_WINDOW_AUTOSIZE);
imshow("Contours", drawing);

cv::namedWindow("img");
cv::imshow("mat", mat);
cv::imshow("mat", origImage);
cv::imshow("mat123", drawing);
cv::waitKey(0);

这给出了这个结果:

original image with contours

Largest contour detected

您可以在底部图像中看到最大的等高线周围绘制了一个棕色矩形。

o 显然,一旦您拥有最大的 blob(或您认为“正确的 blob”),您就可以将其他所有内容设置为黑色,这非常简单。

关于OpenCV:如何删除不需要的 Blob 或如何将想要的部分复制到空图像中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49401636/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com