前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OpenCV实现图像物体轮廓,前景背景,标记,并保存。

OpenCV实现图像物体轮廓,前景背景,标记,并保存。

作者头像
流川疯
发布2019-01-18 14:16:10
9430
发布2019-01-18 14:16:10
举报
#include <iostream> // for standard I/O
#include <string>   // for strings
#include <iomanip>  // for controlling float print precision 
#include <sstream>  // string to number conversion 

#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>  // OpenCV window I/O



using namespace std;
using namespace cv;


// images
Mat inputImg, showImg, segMask, segShowImg;

// mask
Mat fgScribbleMask, bgScribbleMask;


// user clicked mouse buttons flags
bool rButtonDown = false;
bool lButtonDown = false;
int scribbleRadius = 5;


// mouse listener
static void onMouse( int event, int x, int y, int, void* )
{
	//cout << "On Mouse: (" << x << "," << y << ")" <<endl;


	if (event == CV_EVENT_LBUTTONDOWN)
	{
		lButtonDown = true;

	}
	else if (event == CV_EVENT_RBUTTONDOWN)
	{
		rButtonDown = true;

	}
	else if (event == CV_EVENT_LBUTTONUP)
	{
		lButtonDown = false;
	}
	else if (event == CV_EVENT_RBUTTONUP)
	{
		rButtonDown = false;
	}
	else if (event == CV_EVENT_MOUSEMOVE)
	{
		if (rButtonDown)
		{
			// scribble the background

			circle(bgScribbleMask,Point(x,y),scribbleRadius, 255,-1);
			circle(showImg,Point(x,y),scribbleRadius, CV_RGB(0,0,255),-1);

		}
		else if (lButtonDown)
		{
			// scribble the foreground

			circle(fgScribbleMask,Point(x,y),scribbleRadius, 255,-1);
			circle(showImg,Point(x,y),scribbleRadius, CV_RGB(255,0,0),-1);

			//fgScribbleMask.at<char>(y,x)=(char)255;
			// set variables using mask
			//showImg.setTo(redColorElement,fgScribbleMask);

			//showImg.at<Vec3b>(y,x)[0] = 0;
			//showImg.at<Vec3b>(y,x)[1] = 0;
			//showImg.at<Vec3b>(y,x)[2] = 255;
		}

	}


	imshow("Scribble Image", showImg);
	imshow("fg mask", fgScribbleMask);
	imshow("bg mask", bgScribbleMask);
}


// clear everything before closing
void destroyAll()
{
	// destroy all windows
	destroyWindow("Input Image");
	destroyWindow("Scribble Image");
	
	
	destroyWindow("bg mask");
	destroyWindow("fg mask");
	destroyWindow("Segmentation Mask");
	destroyWindow("Segmentation Image");

	// clear all data
	fgScribbleMask.release();
	bgScribbleMask.release();
	inputImg.release();
	showImg.release();
	
	segMask.release();
	segShowImg.release();

	

}

// init all images/vars
int init(char * imgFileName)
{
	// Read the file
	inputImg = imread(imgFileName, CV_LOAD_IMAGE_COLOR);   
	showImg = inputImg.clone();
	segShowImg = inputImg.clone();



	// Check for invalid input
	if(!inputImg.data )                              
	{
		cout <<  "Could not open or find the image: " << imgFileName << std::endl ;
		return -1;
	}

	// this is the mask to keep the user scribbles
	fgScribbleMask.create(2,inputImg.size,CV_8UC1);
	fgScribbleMask = 0;
	bgScribbleMask.create(2,inputImg.size,CV_8UC1);
	bgScribbleMask = 0;
	segMask.create(2,inputImg.size,CV_8UC1);
	segMask = 0;
	

	// Create a window for display.
	namedWindow( "Input Image", CV_WINDOW_AUTOSIZE );
	namedWindow( "Scribble Image", CV_WINDOW_AUTOSIZE);

	namedWindow( "fg mask", CV_WINDOW_AUTOSIZE );
	namedWindow( "bg mask", CV_WINDOW_AUTOSIZE );


	// Show our image inside it.
	imshow( "Input Image", inputImg );                        
	imshow( "Scribble Image", showImg );  

	imshow("fg mask", fgScribbleMask);
	imshow("bg mask", bgScribbleMask);


	moveWindow("Scribble Image", 1,1);
	moveWindow("Input Image", inputImg.cols + 50,1);
	moveWindow("Bin Per Pixel", 2*(inputImg.cols + 50),1);
	moveWindow("Edges", 2*(inputImg.cols + 55),1);


	// set the callback on mouse
	setMouseCallback("Scribble Image", onMouse, 0);


	return 0;
}


int main(int argc, char *argv[])
{

	String image_name,numBinsStr,bhaSlopeStr;
	cout<<"input Parameters:"<<endl;
	cout<<"image name: ";
	cin>>image_name;
	

	// get img name parameter
	char * imgFileName = (char *)image_name.c_str();


	if (init(imgFileName)==-1)
	{
		cout <<  "Could not initialize" << endl ;
		return -1;
	}



	// Wait for a keystroke in the window
	for (;;)
	{
		char key = waitKey(0);                          
		switch (key)
		{
		case 'q':
			cout << "goodbye" << endl;
			destroyAll();
			return 0;
		case '-':
			//缩小画笔直径
			if (scribbleRadius > 2)
				scribbleRadius --;
			cout << "current radius is " << scribbleRadius << endl;
			break;
		case '+':
			if (scribbleRadius < 100)
				scribbleRadius ++;
			cout << "current radius is " << scribbleRadius << endl;
			break;
		case 's':
			{
				
	
				// this is where we store the results
				segMask = 0;
				inputImg.copyTo(segShowImg);
				//inputImg.copyTo(showImg);


				imwrite("bg.bmp",bgScribbleMask);

				break;

			}
		case 'r':
			{
				cout << "resetting" << endl;
				destroyAll();
				if (init(imgFileName)==-1)
				{
					cout <<  "could not initialize" << std::endl ;
					return -1;
				}
				break;
			}
		}
	}


	return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014年09月10日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档