#include
#include
using namespace cv;
using namespace std;
void onMouse(int event, int x, int y, int flags, void* param); //evnet:鼠标事件类型 x
2、创建鼠标操作函数的源文件:onMouse.cpp
#include "onMouse.h"
void onMouse(int event, int x, int y, int flags, void* param) //evnet:鼠标事件类型 x,y:鼠标坐标 flags:鼠标哪个键
{
Mat* im = reinterpret_cast(param);
switch (event) {
case EVENT_LBUTTONDOWN:
//显示图像像素值
if (static_cast(im->channels()) == 1)
{
//若图像为灰度图像,则显示鼠标点击的坐标以及灰度值
cout << "at (" << x << ", " << y << " ) value is: " << static_cast(im->at(Point(x, y))) << endl;
}
else
{
//若图像为彩色图像,则显示鼠标点击坐标以及对应的B, G, R值
cout << "at (" << x << ", " << y << ")"
<< " B value is: " << static_cast(im->at(Point(x, y))[0])
<< " G value is: " << static_cast(im->at(Point(x, y))[1])
<< " R value is: " << static_cast(im->at(Point(x, y))[2])
<< endl;
}
break;
}
}
3、示例
#include
#include
#include "onMouse.h"
using namespace cv;
using namespace std;
int main()
{
Mat image1 = imread("lena.png"); //读取图像;
if (image1.empty())
{
cout << "读取错误" << endl;
return -1;
}
imshow("image1",image1); //显示图像;
setMouseCallback("image1", onMouse, reinterpret_cast(&image1)); //关联图像显示窗口和onMouse函数
waitKey(0); //暂停,保持图像显示,等待按键结束
return 0;
}
#include
#include
#include
using namespace std;
using namespace cv;
int main(int argc, char* argv[])
{
const char* imagename = "2.jpg";
//从文件中读入图像
Mat img = imread(imagename);
ofstream outfile("rgb.txt");
//如果读入图像失败
if (img.empty())
{
fprintf(stderr, "Can not load image %s\n", imagename);
return -1;
}
int i, j;
int cPointR, cPointG, cPointB, cPoint;//currentPoint;
for (i = 1; i < img> {
for (j = 1; j {> >(/> >(/> >(/> /> /> >/> {> >(/> >(/> >(/> }> }> /> }>>< style> //显示图像
imshow("image", img);
//此函数等待按键,按键盘任意键就返回
waitKey();
return 0;
}
opencv获得鼠标点击像素坐标
鼠标操作涉及两个函数,第一个是cvSetMouseCallback,这个函数注册一个回调函数。使用方法如下:
cvSetMouseCallback(“TestWin”, TestCallBack, Context);
前两个参数分别是窗口的名字和回调函数,最后一个参数需要稍微解释一下,这个参数会作为参数传递给回调函数,从而也就可以在主进程与回调函数之间交换上下文。这样就不需要设置大量的全局变量而破坏了整体程序的结构。
鼠标操作需要的回调函数是由开发者自己编写的,这个函数具有下面的原型:
void OnMouse(int event, int x, int y, int flags, void* param);
观察这个函数的参数,第一个参数是event表达了当前的鼠标事件,鼠标事件的类型有如下几种:
#define CV_EVENT_MOUSEMOVE 0
#define CV_EVENT_LBUTTONDOWN 1
#define CV_EVENT_RBUTTONDOWN 2
#define CV_EVENT_MBUTTONDOWN 3
#define CV_EVENT_LBUTTONUP 4
#define CV_EVENT_RBUTTONUP 5
#define CV_EVENT_MBUTTONUP 6
#define CV_EVENT_LBUTTONDBLCLK 7
#define CV_EVENT_RBUTTONDBLCLK 8
#define CV_EVENT_MBUTTONDBLCLK 9
定义的名字已经很好解释了其中的定义。第二个、第三个参数给出了当前鼠标的位置。需要注意的是,这个位置不是相对于窗口或者整个屏幕的,而是相对于Image的。当然这个本身和OpenCV显示的方式是一致的,毕竟我们对于窗口的操作是在Image的基础上完成的。
第三个参数告诉我们当前按键的情况,具体标志如下:
#define CV_EVENT_FLAG_LBUTTON 1
#define CV_EVENT_FLAG_RBUTTON 2
#define CV_EVENT_FLAG_MBUTTON 4
#define CV_EVENT_FLAG_CTRLKEY 8
#define CV_EVENT_FLAG_SHIFTKEY 16
#define CV_EVENT_FLAG_ALTKEY 32
很容易看出,只需要执行(flag & CV_EVENT_FLAG_LBUTTON)的操作就可以检测左键是否按下,对于其他按键也是类似的。
代码
#include
#include
#include
using namespace cv;
using namespace std;
Mat *img = 0;
void onMouse(int event, int x, int y, int flags, void* param)
{
Mat *im = reinterpret_cast(param);
switch (event)
{
case 1: //鼠标左键按下响应:返回坐标和灰度
std::cout << "at(" << x>
<< static>(im->at(cv::Point(x, y))) << std>
break;
case 2: //鼠标右键按下响应:输入坐标并返回该坐标的灰度
std::cout << "input(x,y)" << endl>
std::cout << "x =" << endl>
cin >> x;
std::cout << "y =" << endl>
cin >> y;
std::cout << "at(" << x>
<< static>(im->at(cv::Point(x, y))) << std>
break;
}
}
int main()
{
Mat src = imread("1.jpg");
img = &src;
Mat src2 = src.clone();
namedWindow("original image", WINDOW_AUTOSIZE);
cv::setMouseCallback("original image", onMouse, reinterpret_cast (img));//注册鼠标操作(回调)函数
imshow("original image", src);
waitKey();
return 0;
}
#include “stdafx.h”
#include"cv.h"
#include
#include
#include
#include
#include
#include “iostream”
#include
#include
using namespace std;
int main()
{
IplImage* img = 0;
int height,width,step,channels;
int i,j,k;
img = cvLoadImage(“k:\lena.jpg”,-1);
CvScalar s;
height = img->height;
width = img->width;
ofstream outFile_B;
ofstream outFile_G;
ofstream outFile_R;
outFile_B.open(“k:\out_B.txt”);
outFile_G.open(“k:\out_G.txt”);
outFile_R.open(“k:\out_R.txt”);
//get the pixel value
//CvScalar s;
for(i=0;i
{
for(j=0;j
{
s=cvGet2D(img,i,j); // get the (j,i) pixel value
outFile_B<
outFile_G<
outFile_R<
}
outFile_B<
outFile_G<
outFile_R<
}
return 0;
}
1、创建鼠标操作函数的头文件:onMouse.h
2、创建鼠标操作函数的源文件:onMouse.cpp
3、示例
相关问题推荐
换行。比如,print hello\nworld效果就是helloworld\n就是一个换行符。\是转义的意思,'\n'是换行,'\t'是tab,'\\'是,\ 是在编写程序中句子太长百,人为换行后加上\但print出来是一整行。...
十种常见排序算法一般分为以下几种:(1)非线性时间比较类排序:a. 交换类排序(快速排序、冒泡排序)b. 插入类排序(简单插入排序、希尔排序)c. 选择类排序(简单选择排序、堆排序)d. 归并排序(二路归并排序、多路归并排序)(2)线性时间非比较类排序:...
前景很好,中国正在产业升级,工业机器人和人工智能方面都会是强烈的热点,而且正好是在3~5年以后的时间。难度,肯定高,要求你有创新的思维能力,高数中的微积分、数列等等必须得非常好,软件编程(基础的应用最广泛的语言:C/C++)必须得很好,微电子(数字电...
迭代器与生成器的区别:(1)生成器:生成器本质上就是一个函数,它记住了上一次返回时在函数体中的位置。对生成器函数的第二次(或第n次)调用,跳转到函数上一次挂起的位置。而且记录了程序执行的上下文。生成器不仅记住了它的数据状态,生成器还记住了程序...
python中title( )属于python中字符串函数,返回’标题化‘的字符串,就是单词的开头为大写,其余为小写
第一种解释:代码中的cnt是count的简称,一种电脑计算机内部的数学函数的名字,在Excel办公软件中计算参数列表中的数字项的个数;在数据库( sq| server或者access )中可以用来统计符合条件的数据条数。函数COUNT在计数时,将把数值型的数字计算进去;但是...
head是方法,所以需要取小括号,即dataset.head()显示的则是前5行。data[:, :-1]和data[:, -1]。另外,如果想通过位置取数据,请使用iloc,即dataset.iloc[:, :-1]和dataset.iloc[:, -1],前者表示的是取所有行,但不包括最后一列的数据,结果是个DataFrame。...
挺简单的,其实课程内容没有我们想象的那么难、像我之前同学,完全零基础,培训了半年,直接出来就工作了,人家还在北京大公司上班,一个月15k,实力老厉害了
Python针对众多的类型,提供了众多的内建函数来处理(内建是相对于导入import来说的,后面学习到包package时,将会介绍),这些内建函数功用在于其往往可对多种类型对象进行类似的操作,即多种类型对象的共有的操作;如果某种操作只对特殊的某一类对象可行,Pyt...
相当于 ... 这里不是注释
还有FIXME
python的两个库:xlrd和xlutils。 xlrd打开excel,但是打开的excel并不能直接写入数据,需要用xlutils主要是复制一份出来,实现后续的写入功能。
单行注释:Python中的单行注释一般是以#开头的,#右边的文字都会被当做解释说明的内容,不会被当做执行的程序。为了保证代码的可读性,一般会在#后面加一两个空格然后在编写解释内容。示例:# 单行注释print(hello world)注释可以放在代码上面也可以放在代...
主要是按行读取,然后就是写出判断逻辑来勘测行是否为注视行,空行,编码行其他的:import linecachefile=open('3_2.txt','r')linecount=len(file.readlines())linecache.getline('3_2.txt',linecount)这样做的过程中发现一个问题,...
或许是里面有没被注释的代码
自学的话要看个人情况,可以先在B站找一下视频看一下