opencv图片矩形ROI区域矫正

17 篇文章 0 订阅
10 篇文章 0 订阅
void ImgCorrection(Mat imageSource)
{
	Mat image;
	imageSource.copyTo(image);
	GaussianBlur(image, image, Size(3, 3), 0);  //滤波  
	threshold(image, image, 100, 255, CV_THRESH_BINARY);  //二值化  
	imshow("二值化", image);
	Mat element = getStructuringElement(2, Size(7, 7));  //膨胀腐蚀核  

	dilate(image, image, element);

	imshow("膨胀", image);

	Mat image1;
	Laplacian(image, image1, image.depth(), 1);//拉普拉斯变换寻找边界
	imshow("边界", image1);

	//寻找直线  
	vector<Vec2f>lines;
	HoughLines(image1, lines, 1, CV_PI / 150, 60, 0, 0);
	Mat DrawLine = Mat::zeros(image1.size(), CV_8UC1);
	//Mat DrawLine = Mat::zeros(image1.size(), image1.type());
	for (int i = 0; i<lines.size(); i++)
	{
		float rho = lines[i][0];
		float theta = lines[i][1];
		Point pt1, pt2;
		double a = cos(theta), b = sin(theta);
		double x0 = a*rho, y0 = b*rho;
		pt1.x = cvRound(x0 + 1000 * (-b));
		pt1.y = cvRound(y0 + 1000 * a);
		pt2.x = cvRound(x0 - 1000 * (-b));
		pt2.y = cvRound(y0 - 1000 * a);
		line(DrawLine, pt1, pt2, Scalar(255), 1, CV_AA);
	}
	imshow("直线", DrawLine);

	Point2f P1[4];
	Point2f P2[4];
	vector<Point2f>corners;
	goodFeaturesToTrack(DrawLine, corners, 4, 0.1, 10, Mat()); //角点检测  

	cout << "角点坐标:" << endl;

	for (int i = 0; i<corners.size(); i++)//四个角点的坐标
	{
		circle(DrawLine, corners[i], 3, Scalar(255), 3);
		P1[i] = corners[i];
		cout << corners[i].x << " " << corners[i].y << endl;
	}
	imshow("交点", DrawLine);

	P2[0] = Point2f(0, 0);
	P2[1] = Point2f(0, imageSource.rows);
	P2[2] = Point2f(imageSource.cols, imageSource.rows);
	P2[3] = Point2f(imageSource.cols, 0);
	Mat elementTransf;
	elementTransf = getAffineTransform(P1, P2);
	warpAffine(imageSource, imageSource, elementTransf, imageSource.size(), 1, 0, Scalar(255));
	
	imwrite("corrImge.jpg", imageSource);//校正后的图片保存
	
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
OpenCV是一个开源的计算机视觉库。它提供了许多处理数字图像和视频的函数,而其中之一就是画矩形ROI截图。 首先,要创建一个矩形ROI,需要知道需要截取的区域的左上角和右下角的坐标。可以使用OpenCV自带的鼠标事件函数,在图像上拖动鼠标选择需要截取的区域并获取该区域坐标。 其次,使用cv2.rectangle函数将矩形绘制在图像上。函数参数包括:原始图像,矩形左上角坐标矩形右下角坐标,颜色和线宽度。 最后,使用numpy的切片操作截取原始图像中的ROI区域。切片的参数是左上角和右下角坐标,保持一致性。 示例程序如下: ```python import cv2 import numpy as np # Mouse callback function def draw_rect(event, x, y, flags, params): global x_init, y_init, drawing, top_left_pt, bottom_right_pt if event == cv2.EVENT_LBUTTONDOWN: drawing = True x_init, y_init = x, y elif event == cv2.EVENT_MOUSEMOVE: if drawing: top_left_pt = (min(x_init, x), min(y_init, y)) bottom_right_pt = (max(x_init, x), max(y_init, y)) img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x] elif event == cv2.EVENT_LBUTTONUP: drawing = False top_left_pt = (min(x_init, x), min(y_init, y)) bottom_right_pt = (max(x_init, x), max(y_init, y)) img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x] # MAIN SCRIPT drawing = False top_left_pt, bottom_right_pt = (-1, -1), (-1, -1) # Open image img = cv2.imread('test.png') copy = img.copy() # Create a window cv2.namedWindow("Window") # Set the mouse callback function to "draw_rect" cv2.setMouseCallback("Window", draw_rect) while True: cv2.imshow("Window", img) k = cv2.waitKey(1) & 0xFF # If "c" is pressed, reset the window if k == ord('c'): img = copy.copy() # If "r" is pressed, crop the ROI elif k == ord('r'): if top_left_pt != (-1, -1) and bottom_right_pt != (-1, -1): roi = copy[top_left_pt[1]:bottom_right_pt[1], top_left_pt[0]:bottom_right_pt[0]] cv2.imshow("ROI", roi) # If "q" is pressed, exit elif k == ord('q'): break cv2.destroyAllWindows() ``` 该程序允许用户在图像上拖动鼠标选择需要截取的区域。在选择完区域后,按下“r”键就可以截取该区域并显示在一个新窗口中。按下“c”键可以重置窗口。如果想要结束程序,按下“q”键即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值