OpenCV GUI基本操作,回调函数,进度条,裁剪图像等
代码为转载,出处找不到了,不贴了
工具条进度条:
// ConvertColor.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp> #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
const int slider_max = 100;
int slider;
Mat img;
// Callback function for trackbar event
void 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 <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp> #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 activity
bool ldown = false, lup = false;
// Original image
Mat img;
// Starting and ending points of the user's selection
Point corner1, corner2;
// ROI
Rect box;
// Callback function for mouse events
static 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;
}
裁切效果:
OpenCV GUI基本操作,回调函数,进度条,裁剪图像等的更多相关文章
- 为OLED屏添加GUI支持6:进度条控件
为OLED屏添加GUI支持6:进度条控件 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN10 开发环境:MDK5.13 MCU:S ...
- iOS面向编码|iOSVideoToolbox:读写解码回调函数CVImageBufferRef的YUV图像
iOS面向编码|iOSVideoToolbox:读写解码回调函数CVImageBufferRef的YUV图像 本文档基于H.264的解码,介绍读写Video Toolbox解码回调函数参数CVImag ...
- Opencv step by step - 视频进度条
上一个博文只是进行了视频播放,这里加上进度条. 下面是修改好的代码: #include <cv.h> #include <highgui.h> /* * tan@ubuntu: ...
- Video Toolbox:读写解码回调函数CVImageBufferRef的YUV图像
本文档基于H.264的解码,介绍读写Video Toolbox解码回调函数参数CVImageBufferRef中的YUV或RGB数据的方法,并给出CVImageBufferRef生成灰度图代码.方便调 ...
- OpenCV常用基本处理函数(2)图像基本操作
可以根据像素的行和列的坐标获取他的像素值.对 BGR 图像而言,返回值为 B,G,R 例如获取蓝色的像素值: img=cv2.imread('messi5.jpg')px=img[100,100]bl ...
- OpenCV常用基本处理函数(7)图像金字塔和直方图
高斯金字塔 高斯金字塔的顶部是通过将底部图像中的连续的行和列去除得到的.顶部图像中的每个像素值等于下一层图像中 5 个像素的高斯加权平均值. 这样操作一次一个 MxN 的图像就变成了一个 M/2xN/ ...
- OpenCV常用基本处理函数(6)图像梯度
形态学转换 腐蚀 img = cv2.imread() kernel = np.ones((,),np.uint8) erosion = cv2.erode(img,kernel,iterations ...
- python-----opencv读视频、循环读图片显示进度条
功能:opencv读视频,显示进度条,推动进度条快进.后退,按q退出.代码如下: import os import cv2 def nothing(emp): pass def jindu(name, ...
- 用layer-list实现弧形进度条
本文转载自:http://www.linuxidc.com/Linux/2013-04/82743.htm 之前我有写过如何用style或者是layer-list实现自定义的横向进度条(http:// ...
随机推荐
- [端口扫描]S扫描器跨网段扫描
最近看了下端口扫描,用了几款扫描器,nmap啊,x-sacn等.之前很少关注安全方面的东西,所以也比较菜. 其中有一款叫做 "S扫描器"的,扫描速度非常快,可以大网段的扫描,几十万 ...
- TortoiseSVN使用
TortoiseSVN是Subversion版本控制系统的一个免费开源客户端,不需要为使用它而付费. TortoiseSVN是 Subversion 的 Windows 扩展.它使你避免接触 Subv ...
- 视频编码器评测系统:VideoCodecRank
视频编码器领域一直有个比较复杂的问题:mpeg2.divx.xvid.mpeg4.vp8.vp9.x264.openh264.x265等等这一系列编码器到底哪个好?而对于同一种视频编码器,又包括了各种 ...
- Nginx的负载均衡 - 一致性哈希 (Consistent Hash)
Nginx版本:1.9.1 我的博客:http://blog.csdn.net/zhangskd 算法介绍 当后端是缓存服务器时,经常使用一致性哈希算法来进行负载均衡. 使用一致性哈希的好处在于,增减 ...
- require.js使用步骤
以superagent为例 1.设置lib目录 requirejs.config({ baseUrl: 'libs' }); 2. 使用SuperAgent require(['superagent' ...
- shell的数值计算,小数计算
shell脚本中,可以进行数值计算, 如加减乘除,通过expr.let.(())等完成,文章介绍:http://blog.csdn.net/longshenlmj/article/details/14 ...
- Centos7安装JStorm2.1.1
系统环境 Centos7 外网ip 182.254.145.66 内网ip 10.105.23.114 安装位置 /usr/local/jstorm-2.1.1 安装zookeeper 参见 htt ...
- Socket实现聊天客户端
今天在极客学院上看到了一个关于Socket的视频讲解,感觉还不错,就写了份代码,拿来分享一下. Socket使用方法 关于Socket的使用,我们首先要弄清楚的是,在服务器端还是在客户端使用.因为这的 ...
- Linux:alias永久生效
alias(中文称为"别名")允许使用更加简短的名称来重新定义 Linux 中的 Shell 命令,从而简化命令行的输入. 如果经常与 CLI 打交道,那么使用 alias 不仅会 ...
- 轻松学习Asp.net中的控件
C/S 结构,即大家熟知的客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Server端来实现,降低了系统的通讯开销.目前大多数应用软件 ...