opencv找外接矩形,找的轮廓偏小,怎么解决
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv找外接矩形,找的轮廓偏小,怎么解决相关的知识,希望对你有一定的参考价值。
你可以统计外轮廓上点的坐标啊,然后找出左上角和右下角的点,外接矩就出来了。我把我的代码给你贴出来,不明白了再追问我。CvPoint temp_point;
//初始化角点的坐标,左上角为(0,0),右下角为(height,weight)
int roi_x0 = image->height;
int roi_y0 = image->width;
int roi_x1 = 0;
int roi_y1 = 0;
//遍历轮廓roi_contour上的所有点,并找出左上角和右下角的点
for(int k = 0; k < roi_contour->total; k ++)
temp_point = (CvPoint*) cvGetSeqElem(roi_contour, k);
if(temp_point->x < roi_x0)
roi_x0 = temp_point->x;
if(temp_point->y < roi_y0)
roi_y0 = temp_point->y;
if(temp_point->x > roi_x1)
roi_x1 = temp_point->x;
if(temp_point->y > roi_y1)
roi_y1 = temp_point->y;
CvRect roi_rect;//外接矩
roi_rect.x = roi_x0 ;
roi_rect.y = roi_y0 ;
roi_rect.height = (roi_y1 - roi_y0);
roi_rect.width = (roi_x1 - roi_x0);
cout<<roi_rect.x<<","<<roi_rect.y<<endl;
cout<<roi_rect.height<<","<<roi_rect.width<<endl;
cvRectangle(dst_image,cvPoint(roi_rect.x,roi_rect.y),cvPoint((roi_rect.x+roi_rect.width),(roi_rect.y+roi_rect.height)),CV_RGB(255,255,255),1);//这个语句是画出矩形 参考技术A imgpro(image_source):
wo, ta = image_source.shape[:2]
M = cv2.getRotationMatrix2D((wo / 2, ta / 2), flw, 1)
image_source = cv2.warpAffine(image_source, M, (ta, wo))
dst = cv2.cvtColor(image_source, cv2.COLOR_RGB2GRAY)
ret, dst_2 = cv2.threshold(dst, 90, 230, cv2.THRESH_BINARY)
kernel = np.ones((3, 3), np.uint8)
opening = cv2.morphologyEx(dst_2, cv2.MORPH_OPEN, kernel)
contours, binary = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
src_roi = image_source
for i in range(0, len(contours)):
x, y, w, h = cv2.boundingRect(contours[i])
area = cv2.contourArea(contours[i])
if area < 50000:
continue
cv2.rectangle(image_source, (x, y), (x + w, y + h), (0, 0, 255), 2)
src_roi = image_source[y + 2:y + h - 2, x + 2:x + w - 2]
cv2.imwrite('C:/VisionMaster/img.jpg', src_roi)
break
return src_roi
以上是关于opencv找外接矩形,找的轮廓偏小,怎么解决的主要内容,如果未能解决你的问题,请参考以下文章
我想要在opencv中用最小矩形外接图中图像并剪切截取出这个矩形部分。
youcans 的 OpenCV 例程200篇199.轮廓的外接边界框
Opencv-python 找到图像轮廓并绘制,cv2.findContours()函数,求轮廓外接矩形,cv2.boundingrect()