opencv3鼠標控制閾值滑動條輪廓檢測



#include <stdio.h>  
#include<opencv2\opencv.hpp>
#include<cmath>
#include<iostream>

using namespace cv;
using namespace std;

cv::Mat org_, dst_, img_, tmp_;
int pointValue;
void on_mouse(int event, int x, int y, int flags, void *ustc)//event鼠標事件代號,x,y鼠標座標,flags拖拽和鍵盤操作的代號  
{
	static Point pre_pt = (-1, -1);//初始座標  
	static Point cur_pt = (-1, -1);//實時座標  

	char temp[16];

	if (event == EVENT_LBUTTONDOWN)//左鍵按下,讀取初始座標,並在圖像上該點處劃圓  
	{
		pointValue = 0;
		org_.copyTo(img_);//將原始圖片複製到img中  
		cout << img_.rows << "  " << img_.cols << endl;
		sprintf(temp, "(%d,%d)", x, y);
		pre_pt = Point(x, y);
		pointValue = img_.at<Vec3b>(pre_pt)[0];
		cout << pointValue << endl;
		/*  cout << org.at<Vec3b>(pre_pt)[1];
		cout << org.at<Vec3b>(pre_pt)[2];*/
		//初始座標
		//putText(org_, temp, pre_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255), 1, 8);//在窗口上顯示座標  
		//circle(org_, pre_pt, 2, Scalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);//劃圓  
		imshow("img", org_);
	}
	else if (event == EVENT_MOUSEMOVE && !(flags & EVENT_FLAG_LBUTTON))//左鍵沒有按下的情況下鼠標移動的處理函數  
	{
		img_.copyTo(tmp_);//將img複製到臨時圖像tmp上,用於顯示實時座標  
		sprintf(temp, "(%d,%d)", x, y);
		cur_pt = Point(x, y);
		//putText(org_, temp, cur_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255));//只是實時顯示鼠標移動的座標  
		imshow("img", org_);
	}
	else if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON))//左鍵按下時,鼠標移動,則在圖像上劃矩形  
	{
		img_.copyTo(tmp_);
		sprintf(temp, "(%d,%d)", x, y);
		cur_pt = Point(x, y);
		//putText(org_, temp, cur_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255));
		rectangle(tmp_, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);//在臨時圖像上實時顯示鼠標拖動時形成的矩形  
		//circle(tmp_, pre_pt,scalar)
		imshow("img", tmp_);
	}
	else if (event == EVENT_LBUTTONUP)//左鍵鬆開,將在圖像上劃矩形  
	{

		org_.copyTo(img_);
		sprintf(temp, "(%d,%d)", x, y);
		cur_pt = Point(x, y);
		int height_ = abs(cur_pt.x - pre_pt.x);
		int width_ = abs(cur_pt.y - pre_pt.y);
		//Mat imageBlue, imageGreen, imageRed;
		Mat imageBlue = Mat::zeros(img_.rows, img_.cols, CV_8UC1);
		Mat imageGreen = Mat::zeros(img_.rows, img_.cols, CV_8UC1);
		Mat imageRed = Mat::zeros(img_.rows, img_.cols, CV_8UC1);
		Mat mergeImage;
		Mat test0, test1, test2;
		vector<Mat> channelrgb;
		Mat mer;
		//putText(org_, temp, cur_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255));
		//circle(org_, pre_pt, 2, Scalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
		split(img_, channelrgb);
		imageBlue = channelrgb.at(0);
		imageGreen = channelrgb.at(1);
		imageRed = channelrgb.at(2);
		cout << imageRed.rows << "   " << imageRed.cols << endl;
		if ((cur_pt.x > pre_pt.x) && (cur_pt.y > pre_pt.y))
		{
			for (int i = 0; i < (cur_pt.x - pre_pt.x); i++)
			{
				for (int j = 0; j <(cur_pt.y - pre_pt.y); j++)
				{
					imageBlue.at<uchar>(j + pre_pt.y, i + pre_pt.x) = 0;
					imageGreen.at<uchar>(j + pre_pt.y, i + pre_pt.x) = 0;
					imageRed.at<uchar>(j + pre_pt.y, i + pre_pt.x) = 0;
				}
			}
		}
		channelrgb.at(0) = imageBlue;
		channelrgb.at(1) = imageGreen;
		channelrgb.at(2) = imageRed;
		test0 = channelrgb.at(0);
		test1 = channelrgb.at(1);
		test2 = channelrgb.at(2);
		merge(channelrgb, org_);
		cout << "cui" << endl;
		//截取矩形包圍的圖像,並保存到dst中  
		int width = abs(pre_pt.x - cur_pt.x);
		int height = abs(pre_pt.y - cur_pt.y);
		if (width == 0 || height == 0)
		{
			printf("width == 0 || height == 0");
			return;
		}
		dst_ = org_(Rect(min(cur_pt.x, pre_pt.x), min(cur_pt.y, pre_pt.y), width, height));
		//namedWindow("merge", 0);
		//imshow("merge", org_);
		imwrite("image00012.png", org_);
		waitKey(10);
	}
}

void main()
{

	org_ = imread("2dcm_png/image00012.png");
	namedWindow("img", 0);//定義一個img窗口  
	setMouseCallback("img", on_mouse, 0);//調用回調函數  
										 //imshow("img", img_);

	cv::waitKey(0);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章