【练习4.2】使用鼠标事件获取图片像素值

《学习OpenCV》中文版第4章第2题

题目要求:

点击图片是获取该点的颜色值,并在图像上点击鼠标处用文本将颜色值显示出来。

程序代码:

 1 #include "stdafx.h"
 2 #include <cv.h>
 3 #include <highgui.h>
 4 using namespace std;
 5 using namespace cv;
 6
 7 void MouseCallBack(int event, int x, int y, int flags, void *param);
 8
 9 int _tmain(int argc, _TCHAR* argv[])
10 {
11     const char * fileName = "D:\\Work\\Work_Programming\\Source\\Image\\lena.jpg";
12     cvNamedWindow("MouseEvent", CV_WINDOW_AUTOSIZE);
13     IplImage * img = cvLoadImage(fileName,1);
14     assert(img);
15
16     cvSetMouseCallback("MouseEvent", MouseCallBack, img);
17
18     while (true)
19     {
20         cvShowImage("MouseEvent", img);
21
22         if (waitKey(15) == 27)
23         {
24             break;
25         }
26     }
27
28     cvWaitKey(0);
29
30     cvReleaseImage(&img);
31     cvDestroyWindow("MouseEvent");
32
33     //system("pause");
34
35     return 0;
36 }
37
38 void MouseCallBack(int event, int x, int y, int flags, void *param)
39 {
40     IplImage * img = (IplImage *)param;
41
42     CvFont font;
43     cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 2, 8);
44
45     CvScalar scl;
46     char * str = (char *)malloc(30 * sizeof(char));
47
48     if (event == CV_EVENT_LBUTTONDOWN)
49     {
50         scl = cvGet2D(img, x, y);
51
52         double b = scl.val[0];
53         double g = scl.val[1];
54         double r = scl.val[2];
55
56         sprintf(str, "B:%.0f,G:%.0f,R:%.0f", b, g, r);
57
58         cvPutText(img, str, cvPoint(x, y), &font, cvScalar(255, 0, 0));
59     }
60
61     free(str);
62 }

【练习4.2】使用鼠标事件获取图片像素值

时间: 2024-08-11 09:11:13

【练习4.2】使用鼠标事件获取图片像素值的相关文章

React鼠标事件获取标签属性值

原文地址:https://www.cnblogs.com/orzzt/p/12068045.html

获取修改像素值

1 # -*- coding: utf-8 -*- 2 """ 3 Created on Sun May 5 15:51:34 2019 4 @author: nwpujun 5 #获取修改像素值 6 """ 7 import cv2 8 import numpy as np 9 img=cv2.imread('2018.png') 10 11 px=img[100,100] 12 print px 13 blue=img[100,100,0]

黑马day18 鼠标事件&amp;amp;图片变大

有时候我们在淘宝网或者京东商城上浏览要购买的商品的时候当把鼠标移动到图图片上的时候会发现图片放大.然后鼠标移动,图片也会跟着移动,接下来我就使用jquery来实现这样的效果: 这是图片文件夹: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html x

swift获取图片像素颜色值

extension UIImage{ /** 获取图片中的像素颜色值 - parameter pos: 图片中的位置 - returns: 颜色值 */ func getPixelColor(pos:CGPoint)->(alpha: CGFloat, red: CGFloat, green: CGFloat,blue:CGFloat){ let pixelData=CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage)) let

Python 获取图片像素宽高信息

# coding: utf8 from PIL import Image img = Image.open("img.jpg") print img.size

Opencv读取图片像素值并保存为txt文件

#include <opencv2/opencv.hpp>#include<vector>#include <fstream> using namespace std;using namespace cv; int main(int argc, char* argv[]){ const char* imagename = "2.jpg"; //从文件中读入图像 Mat img = imread(imagename); ofstream outfile

JavaScript-4.6鼠标事件监听,获取鼠标坐标window.event---ShinePans

<html> <head> <meta http-equiv="content-type" content="text/html" charset=GB2312"/> <title> 4.5 window.event应用 </title> <script> function body_onclick(){ alert("鼠标点击的坐标是\r\nx:"+event.x

黑马day18 鼠标事件&amp;图片变大

有时候我们在淘宝网或者京东商城上浏览要购买的商品的时候当把鼠标移动到图图片上的时候会发现图片放大,然后鼠标移动,图片也会跟着移动,接下来我就使用jquery来实现这种效果: 这是图片目录: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xml

BaseTool中鼠标双击事件获取鼠标位置的方法

ArcGISEngine的BaseTool类中提供了鼠标双击.鼠标移动.鼠标按键按下.鼠标按键弹起四种鼠标事件,但是鼠标双击事件的参数跟其他三种不一样,双击事件中没有提供鼠标位置信息. public virtual void OnDblClick(); public virtual void OnMouseDown(int Button, int Shift, int X, int Y); public virtual void OnMouseMove(int Button, int Shift