seb*_*ira 1 c++ opencv object-detection edge-detection canny-operator
我正在学习OpenCV,我已经达到了无论我做什么,我都被困住了.我想要做的是将一个对象(矩形对象)与其背景隔离开来.
我想掩盖该图像,以便剩下的唯一东西就是对象.
我尝试过以下方法:
但我得到一些奇怪的区域作为更大的区域.以下是结果图片:
这是我正在使用的代码:
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
using namespace cv;
using namespace std;
int main( int, char** argv )
{
Mat src, srcGray,srcBlur,srcCanny;
string file = "samsung";
src = imread(file + ".jpg");
cvtColor(src, srcGray, CV_BGR2GRAY);
//bilateralFilter(srcGray, srcBlur,11, 17, 17);
srcBlur = srcGray.clone();
imshow("Filtered", srcBlur);
imwrite(file+"-filtered.jpg",srcBlur);
Canny(srcBlur, srcCanny, 0, 100, 3, true);
imshow("Canny", srcCanny);
imwrite(file+"-canny.jpg",srcCanny);
vector< vector <Point> > contours; // Vector for storing contour
vector< Vec4i > hierarchy;
findContours( srcCanny.clone(), contours, hierarchy,CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
int largest_contour_index=0;
int largest_area=0;
for( int i = 0; i< contours.size(); i++ ){
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
}
}
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0)); //create destination image
drawContours( dst,contours, largest_contour_index, Scalar(255,0,0),CV_FILLED, 8, hierarchy );
imshow("Largest", dst);
imwrite(file+"-largest.jpg",dst);
waitKey();
}
Run Code Online (Sandbox Code Playgroud)
这段代码旨在获取对象的"掩码",然后应该应用掩码,但我无法前进,因为我无法检测到对象
我的目标是在不同的图像中检测矩形对象(每个图像只有一个对象).
这个想法是从这里开始的,但是我无法让代码与像我一样的低对比度图像一起工作.
我也尝试过,这和我想要的几乎一样.
我想隔离一个矩形物体(图像中应该是一个更大的物体)
提前致谢!
PS:虽然我可以将Python翻译成C++但我会直接在C++中得到答案,所以我可以更快地测试它.
这就是我一起攻击的对不起,对不起,这是用Python :)
首先,将图像大小调整为原始大小的1/4(可能无需调整大小即可使用,但使用不同的参数)并应用中值模糊:
w, h, c = img_in.shape #img_in is the input image
resize_coeff = 0.25
img = cv2.resize(img_in, (int(resize_coeff*h), int(resize_coeff*w)))
img = cv2.medianBlur(img, 15)
Run Code Online (Sandbox Code Playgroud)
中值模糊的好处在于它可以消除大部分噪音和微小的不必要的细节,如蓝色标记线,同时保持较大形状的边缘不模糊.现在,让我们应用Canny边缘检测:
img = cv2.Canny(img, 100, 200)
Run Code Online (Sandbox Code Playgroud)
不幸的是,我们的边缘有一些微小的缝隙,但可以用扩张/腐蚀修复:
kernel = np.ones((17, 17), np.uint8)
img = cv2.dilate(img, kernel, 1)
img = cv2.erode(img, kernel, 1)
Run Code Online (Sandbox Code Playgroud)
现在我们可以找到我们的轮廓,按区域选择最大的轮廓,它可能是我们想要的:
img, contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
max_index, max_area = max(enumerate([cv2.contourArea(x) for x in contours]), key = lambda x: x[1])
max_contour = contours[max_index]
Run Code Online (Sandbox Code Playgroud)
将其绘制在原始(缩放)图像之上,我们得到:
img_out = cv2.resize(img_in, (int(resize_coeff*h), int(resize_coeff*w)))
cv2.drawContours(img_out, [max_contour], 0, (0, 0, 255), 2)
Run Code Online (Sandbox Code Playgroud)
通过一些简单的轮廓平滑,如果我们愿意,我们可以很容易地摆脱顶部的电线.不过,不知道如何处理底部的阴影.
| 归档时间: |
|
| 查看次数: |
2302 次 |
| 最近记录: |