OpenCV 中读取一张图片的像素点,并显示

来源(CSDN博客)

From: https://blog.csdn.net/u011559236/article/details/78597766

OpenCV 中读取一张图片的像素点,并显示

菜鸟小馒头 2017-11-21 22:27:47 9742 收藏 8

分类专栏: 图像识别

版权

1、读入并显示一张图像。当用户点击鼠标图像时,获取图像对应像素的(BGR),并在图像上点击鼠标处用文本将颜色值显示出来。

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <stdio.h>
#include <cstdlib>
 
using namespace std;
using namespace cv;
 
void my_mouse_callback(int event,int x, int y, int flags, void *param);
 
CvPoint st_point;
int blue  = 0;
int green = 0;
int red   = 0;
CvFont font;
 
int main(int argc, const char * argv[]) {
    const char filename[] = "/Users/linwang/Desktop/Lena.png";
    
    IplImage * image = cvLoadImage(filename);
    
    cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX, 1, 1, 0, 2, 8);
    
    cvNamedWindow("Lena");
    
    cvSetMouseCallback("Lena",  my_mouse_callback, (void *)image);
    
    cvShowImage("Lena", image);
    
    cvWaitKey(0);
 
    cvReleaseImage(&image);
    
    cvDestroyWindow("Lena");
}
 
void my_mouse_callback(int event,int x,int y,int flags,void *param)
{
    IplImage * image = (IplImage *)param;
    
    switch (event) {
            /*鼠标离开*/
        case CV_EVENT_LBUTTONDOWN:
            {
                /*鼠标左键点击事件,记录点击的(x,y)*/
                st_point.x = x;
                st_point.y = y;
                cout<<"按下左键 : "<<" x = "<<x <<" y "<<y<<endl;
                break;
            }
        case CV_EVENT_LBUTTONUP:
            {
                /*鼠标左键放开事件,读取当前点的像素值BGR*/
                unsigned char * ptr = (uchar *)(image->imageData + st_point.x * image->widthStep);
                blue  = (int)(*(ptr + st_point.y));
                green = (int)(*(ptr + st_point.y + 1));
                red   = (int)(*(ptr + st_point.y + 2));
                cout<<"抬起左键 : "<<" blue= "<<blue<<" green = "<<green<<" red =  "<<red<<endl;
                char text[20] = "0";
                sprintf(text,"(%d,%d,%d)",blue,green,red);
                cout<<"Text = "<<text<<endl;
                cvPutText(image, text, st_point, &font, CV_RGB(0,0,0));
                cvShowImage("Lena", image);
                break;
            }
        default:
            break;
    }
}




Link: http://www.asm32.net/article_details.aspx?id=7376


浏览次数 0 发布时间 2020-07-31 13:13:01 从属分类 C/C++ 【评论】【 】【打印】【关闭
 
| www.asm32.net | 2006版 | 资料中心 | linux | asm/asm32 | C/C++ | VC++ | java | Python | 书签 | ASP.Net书签 | 京ICP备09029108号-1