opencv notes (1) cutout

Any shape cutout

#include <iostream>  
#include <vector>
#include <opencv2\opencv.hpp>
#include <opencv2/highgui/highgui.hpp>    
#include <opencv2/core/core.hpp>
#define CV_AA 16

using namespace cv;
static std::vector<std::vector<cv::Point>> vctvctPoint;
cv::Mat org = cv::imread("images/kaola.jpg");
cv::Mat dst, maskImage;
static std::vector<cv::Point> vctPoint;
static cv::Point ptStart = (-1, -1); //初始化起点
static cv::Point cur_pt = (-1, -1);  //初始化临时节点
char temp[16];


void on_mouse(int event, int x, int y, int flags, void *ustc)//event鼠标事件代号,x,y鼠标坐标,flags拖拽和键盘操作的代号    
{
	if (event == EVENT_LBUTTONDOWN)
	{
		std::cout << "x:" << x << " y:" << y << std::endl;
		ptStart = cv::Point(x, y);
		vctPoint.push_back(ptStart);
		cv::circle(org, ptStart, 1, cv::Scalar(255, 0, 255), FILLED, CV_AA, 0);
		cv::imshow("图片", org);
		//cv::putText(tmp, temp, ptStart, cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0, 0), 1, 8);
	}
	else if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON))
	{
		std::cout << "x:" << x << " y:" << y << std::endl;
		cur_pt = cv::Point(x, y);
		cv::line(org, vctPoint.back(), cur_pt, cv::Scalar(0, 255, 0, 0), 1, 8, 0);
		cv::circle(org, cur_pt, 1, cv::Scalar(255, 0, 255), FILLED, CV_AA, 0);
		cv::imshow("图片", org);
		vctPoint.push_back(cur_pt);
		//cv::putText(tmp, temp, cur_pt, cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0, 0));
	}
	else if (event == EVENT_LBUTTONUP)
	{
		std::cout << "x:" << x << " y:" << y << std::endl;
		cur_pt = cv::Point(x, y);
		cv::line(org, ptStart, cur_pt, cv::Scalar(0, 255, 0, 0), 1, 8, 0);
		cv::circle(org, cur_pt, 1, cv::Scalar(255, 0, 255), FILLED, CV_AA, 0);
		cv::imshow("图片", org);
		vctPoint.push_back(cur_pt);
		vctvctPoint.push_back(vctPoint);
		//把点构成任意多边形进行填充
		const cv::Point * ppt[1] = { &vctPoint[0] };//取数组的首地址
		int len = vctPoint.size();
		int npt[] = { len };
		//	cv::polylines(org, ppt, npt, 1, 1, cv::Scalar(0,0, 0, 0), 1, 8, 0);	    
		org.copyTo(maskImage);
		maskImage.setTo(cv::Scalar(0, 0, 0, 0));
		cv::fillPoly(maskImage, ppt, npt, 1, cv::Scalar(0, 255, 255, 255));
		org.copyTo(dst, maskImage);
		cv::imshow("抠图", dst);
		cv::waitKey(0);
	}
}
int main()
{
	//鼠标点击
	cv::namedWindow("图片");//定义一个img窗口    
	cv::setMouseCallback("图片", on_mouse, 0);//调用回调函数    
	cv::imshow("图片", org);
	cv::waitKey(0);
	return 0;
}

 

Reference blog: Opencv use-use the mouse to cut out any shape of the picture (get a free shape mask)

Reference blog: opencv use mouse to cut out any shape

Mouse selected quadrilateral perspective transformation

#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

struct userdata
{
	Mat img;
	vector<Point2f> points;
};


void mouseHandler(int event, int x, int y, int flags, void* data_ptr)
{
	if (event == EVENT_LBUTTONDOWN)
	{
		userdata* data = ((userdata*)data_ptr);
		circle(data->img, Point(x, y), 3, Scalar(0, 255, 255), 5);
		imshow("Image_window", data->img);//用同一窗口取点画点

		if (data->points.size() < 4)
		{
			data->points.push_back(Point2f(x, y));
		}
	}
}

int main(int argc, char** argv)
{
	//输出目标矩形,和四个顶点。
	//四个顶点是按环形顺序排列的,因此在鼠标选点时也要对应环形取点
	Mat outRect(360, 480, CV_8UC3);
	vector<Point2f> dst_points;
	dst_points.push_back(Point2f(0, 0));
	dst_points.push_back(Point2f(outRect.cols, 0));
	dst_points.push_back(Point2f(outRect.cols, outRect.rows));
	dst_points.push_back(Point2f(0, outRect.rows));

	Mat im_src = imread("images/sh.jpg");
	imshow("Image_window", im_src);

	userdata data;
	data.img = im_src.clone();  //不clone原图就被画点了
								//set the callback function for any mouse event
	setMouseCallback("Image_window", mouseHandler, &data);
	waitKey(0);

	//	Mat warpMatrix = findHomography(data.points, dst_points);
	Mat warpMatrix = getPerspectiveTransform(data.points, dst_points);

	warpPerspective(im_src, outRect, warpMatrix, outRect.size(), INTER_LINEAR, BORDER_CONSTANT);

	imwrite("outRect.jpg", outRect);//保存矩形图片
	imshow("outRect", outRect);
	waitKey(0);

	return 0;
}

Refer to this code to write an annotation tool for any quadrilateral annotation.

Reference blog: opencv perspective transformation (2) pull patch-save the quadrilateral area in the image as a rectangle

 

 

 

Guess you like

Origin blog.csdn.net/juluwangriyue/article/details/109087242