博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OpenCV GUI基本操作,回调函数,进度条,裁剪图像等
阅读量:4612 次
发布时间:2019-06-09

本文共 3219 字,大约阅读时间需要 10 分钟。

代码为转载,出处找不到了,不贴了

 

工具条进度条:

// ConvertColor.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include 
#include
#include
#pragma comment(lib,"opencv_core2410d.lib") #pragma comment(lib,"opencv_highgui2410d.lib") #pragma comment(lib,"opencv_imgproc2410d.lib") using namespace std;using namespace cv;// Global variablesconst int slider_max = 100;int slider;Mat img;// Callback function for trackbar eventvoid on_trackbar(int pos, void *){ Mat img_converted; if(pos > 0) cvtColor(img, img_converted, CV_RGB2GRAY); else img_converted = img; imshow("Trackbar app", img_converted);}int main(){ img = imread("swan.jpg"); namedWindow("Trackbar app"); imshow("Trackbar app", img); slider = 0; createTrackbar("RGB <-> Grayscale", "Trackbar app", &slider, slider_max, on_trackbar); while(char(waitKey(1)) != 'q') {} return 0;}

 

效果:

 

 

 

 

图像裁切代码:

 

// ConvertColor.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include 
#include
#include
#pragma comment(lib,"opencv_core2410d.lib") #pragma comment(lib,"opencv_highgui2410d.lib") #pragma comment(lib,"opencv_imgproc2410d.lib") using namespace std;using namespace cv;// Global variables// Flags updated according to left mouse button activitybool ldown = false, lup = false;// Original imageMat img;// Starting and ending points of the user's selectionPoint corner1, corner2;// ROIRect box;// Callback function for mouse eventsstatic void mouse_callback(int event, int x, int y, int, void *){ // When the left mouse button is pressed, record its position and save it in corner1 if(event == EVENT_LBUTTONDOWN) { ldown = true; corner1.x = x; corner1.y = y; cout << "Corner 1 recorded at " << corner1 << endl; } // When the left mouse button is released, record its position and save it in corner2 if(event == EVENT_LBUTTONUP) { // Also check if user selection is bigger than 20 pixels (jut for fun!) if(abs(x - corner1.x) > 20 && abs(y - corner1.y) > 20) { lup = true; corner2.x = x; corner2.y = y; cout << "Corner 2 recorded at " << corner2 << endl << endl; } else { cout << "Please select a bigger region" << endl; ldown = false; } } // Update the box showing the selected region as the user drags the mouse if(ldown == true && lup == false) { Point pt; pt.x = x; pt.y = y; Mat local_img = img.clone(); rectangle(local_img, corner1, pt, Scalar(0, 0, 255)); imshow("Cropping app", local_img); } // Define ROI and crop it out when both corners have been selected if(ldown == true && lup == true) { box.width = abs(corner1.x - corner2.x); box.height = abs(corner1.y - corner2.y); box.x = min(corner1.x, corner2.x); box.y = min(corner1.y, corner2.y); // Make an image out of just the selected ROI and display it in a new window Mat crop(img, box); namedWindow("Crop"); imshow("Crop", crop); ldown = false; lup = false; }}int main(){ img = imread("swan.jpg"); namedWindow("Cropping app"); imshow("Cropping app", img); // Set the mouse event callback function setMouseCallback("Cropping app", mouse_callback); // Exit by pressing 'q' while(char(waitKey(1)) != 'q') {} return 0;}

 

 

裁切效果:

 

转载于:https://www.cnblogs.com/wuyida/p/6301386.html

你可能感兴趣的文章
视频转gif
查看>>
KakfaSpout自定义scheme
查看>>
java基础之循环应用(打印三角形、菱形)
查看>>
loadrunner监控tomcat 代码及遇到的问题
查看>>
广度优先搜索 codevs 2806 红与黑
查看>>
人生第一次博客
查看>>
mysql 动态行转列
查看>>
矢量图标
查看>>
类及成员的属性——静态成员与实例成员
查看>>
float 的不确定性
查看>>
PHP 错误日志/安全配置
查看>>
挂载一个NFS共享
查看>>
C语言数据结构之线性表的基本操作
查看>>
html5getcontext()使用与说明
查看>>
Oracle游标使用
查看>>
mysql判断两个时间段是否有交集
查看>>
javascript 获取视口的高度和宽度
查看>>
快速入门git第六步
查看>>
bzoj3295: [Cqoi2011]动态逆序对
查看>>
labview 变体数据类型
查看>>