设置鼠标callback setMouseCallback()
| |
| void cv::setMouseCallback( const String& windowName, MouseCallback onMouse, void* param) |
| { |
| CV_TRACE_FUNCTION(); |
| cvSetMouseCallback(windowName.c_str(), onMouse, param); |
| } |
| |
| |
| |
| |
| void On_MouseHandle() |
| { |
| |
| |
| } |
mat 操作
| |
| Mat A,C; |
| |
| A= imread("1.jpg",CV_LOAD_IMAGE_COLOR); |
| |
| Mat B(A); |
| |
| C=A; |
| |
| Mat D(A,Rect(10,10,100,100)); |
| |
| Mat E=A(Range:all(),Range(1,3)); |
| |
| |
| Mat F = A.clone(); |
| Mat G; |
| A.copyTo(G); |
| 方法1: 使用Mat()构造函数 |
| |
| Mat M( 2, 2, CV_8UC3, Scalar(0,0,255)); |
| cout << "M=" <<endl; <<" " << M<<endl <<endl; |
| |
| CV_8UC3: |
| - 8 位 |
| - UC 带符号 |
| - C 通道数量(channel) |
| |
| |
| 方法2:在C/C++ 中通过构造函数进行初始化 |
| |
| int sz[3] ={2,2,2}; |
| Mat L(3,sz,CV_8UC, Scalar::all(0)); |
| |
| 创建一个超过2维的矩阵, |
| 指定维度数,然后传递一个指向一个数组的指针,这个数组包含每个维度的尺寸。 |
| |
| |
| |
| 方法3: 为已存在的IplImage指针创建信息头 |
| |
| IplImage* img = cvLoadImage("1.jpg", 1); |
| Mat mtx(img); |
| |
| 方法4: Create() |
| |
| M.create( 4,4, CV_8UC(2)); |
| |
| cout << "M="<< endl<<" "<<M<<endl<<endl; |
| |
| 方法5: 采用Matlab式的初始化方式 |
| |
| zeros(), ones(),eyes() |
| |
| 使用以下方式指定储存和数据类型 |
| |
| Mat E =Mat::eye(4,4,CV_64F); |
| cout<<"E= "<<endl<<" "<<E<<endl<<endl; |
| |
| Mat O = Mat::ones(2,2,CV_32F); |
| cout<<"O= "<<endl<<" "<<O<<endl<<endl; |
| |
| Mat Z = Mat::zeros(3,3,CV_8UC1); |
| cout<<"Z= "<<endl<<" "<<Z<<endl<<endl; |
| |
| 方法6: 对于小矩阵,使用都好分隔 初始化函数 |
| |
| Mat C =(Mat_<double>(3,3)) <<0,-1,0,-1,5,-1,0,-1,0); |
| cout<<"C= "<<endl<<" "<<C<<endl<<endl; |
| |
| 显示:C = [0,-1, 0; |
| -1,5,-1; |
| 0,-1,0] |
| |
| 方法7: 为已存在的对象穿件新信息头 |
| |
| Mat RowClone = C.row(1).clone(); |
| cout<<"RowClone= "<<endl<<" "<<RowClone<<endl<<endl; |
| |
| 显示:RowClone = [-1,5,-1] |
example: image.cpp
| #include <stdio.h> |
| #include <iostream> |
| #include <opencv2/imgproc.hpp> |
| #include <opencv2/highgui.hpp> |
| #include <opencv2/core/utility.hpp> |
| |
| using namespace cv; |
| using namespace std; |
| |
| static void help() |
| { |
| cout << |
| "\nThis program shows how to use cv::Mat and IplImages converting back and forth.\n" |
| "It shows reading of images, converting to planes and merging back, color conversion\n" |
| "and also iterating through pixels.\n" |
| "Call:\n" |
| "./image [image-name Default: ../data/lena.jpg]\n" << endl; |
| } |
| |
| |
| #define DEMO_MIXED_API_USE 1 |
| |
| |
| |
| #ifdef DEMO_MIXED_API_USE |
| # include <opencv2/highgui/highgui_c.h> |
| # include <opencv2/imgcodecs/imgcodecs_c.h> |
| #endif |
| |
| int main( int argc, char** argv ) |
| { |
| cv::CommandLineParser parser(argc, argv, "{help h | |}{@image|../data/lena.jpg|}"); |
| if (parser.has("help")) |
| { |
| help(); |
| return 0; |
| } |
| string imagename = parser.get<string>("@image"); |
| #if DEMO_MIXED_API_USE |
| |
| Ptr<IplImage> iplimg(cvLoadImage(imagename.c_str())); |
| if(!iplimg) |
| { |
| fprintf(stderr, "Can not load image %s\n", imagename.c_str()); |
| return -1; |
| } |
| Mat img = cv::cvarrToMat(iplimg); |
| |
| |
| |
| #else |
| Mat img = imread(imagename); |
| if(img.empty()) |
| { |
| fprintf(stderr, "Can not load image %s\n", imagename.c_str()); |
| return -1; |
| } |
| #endif |
| |
| if( img.empty() ) |
| return -1; |
| |
| Mat img_yuv; |
| cvtColor(img, img_yuv, COLOR_BGR2YCrCb); |
| |
| vector<Mat> planes; |
| split(img_yuv, planes); |
| |
| #if 1 |
| |
| MatIterator_<uchar> it = planes[0].begin<uchar>(), it_end = planes[0].end<uchar>(); |
| for(; it != it_end; ++it) |
| { |
| double v = *it*1.7 + rand()%21-10; |
| *it = saturate_cast<uchar>(v*v/255.); |
| } |
| |
| |
| |
| for( int y = 0; y < img_yuv.rows; y++ ) |
| { |
| uchar* Uptr = planes[1].ptr<uchar>(y); |
| for( int x = 0; x < img_yuv.cols; x++ ) |
| { |
| Uptr[x] = saturate_cast<uchar>((Uptr[x]-128)/2 + 128); |
| uchar& Vxy = planes[2].at<uchar>(y, x); |
| Vxy = saturate_cast<uchar>((Vxy-128)/2 + 128); |
| } |
| } |
| |
| #else |
| Mat noise(img.size(), CV_8U); |
| randn(noise, Scalar::all(128), Scalar::all(20)); |
| |
| GaussianBlur(noise, noise, Size(3, 3), 0.5, 0.5); |
| |
| const double brightness_gain = 0; |
| const double contrast_gain = 1.7; |
| #if DEMO_MIXED_API_USE |
| |
| |
| IplImage cv_planes_0 = planes[0], cv_noise = noise; |
| |
| cvAddWeighted(&cv_planes_0, contrast_gain, &cv_noise, 1, -128 + brightness_gain, &cv_planes_0); |
| #else |
| addWeighted(planes[0], contrast_gain, noise, 1, -128 + brightness_gain, planes[0]); |
| #endif |
| const double color_scale = 0.5; |
| |
| planes[1].convertTo(planes[1], planes[1].type(), color_scale, 128*(1-color_scale)); |
| |
| |
| planes[2] = Mat_<uchar>(planes[2]*color_scale + 128*(1-color_scale)); |
| |
| |
| planes[0] = planes[0].mul(planes[0], 1./255); |
| #endif |
| |
| |
| merge(planes, img_yuv); |
| |
| cvtColor(img_yuv, img, COLOR_YCrCb2BGR); |
| |
| |
| namedWindow("image with grain", WINDOW_AUTOSIZE); |
| #if DEMO_MIXED_API_USE |
| |
| |
| cvShowImage("image with grain", iplimg); |
| #else |
| imshow("image with grain", img); |
| #endif |
| waitKey(); |
| |
| return 0; |
| |
| } |
| |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2018-11-04 Jlink flash 烧录HEX 程序
2015-11-04 process thread Fiber(linux)