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:// ...
随机推荐
- Android学习路线指南
看到这位大牛的博文,不禁得感概,我最近也遇到了很多问题,内心彷徨不堪,转载大牛这篇博文,是为了更好的勉励自己.原文地址在最后面. 前言 看到一篇文章中提到"最近几年国内的初级Android程 ...
- JDBC数据库连接简介(一)
jdbc的由来 odbc(open database connection) 最初各个数据库比如mysql和oracle等,虽然都支持sql,但是他们的连接方式是不一样的,需要按照相应的api来编写不 ...
- 直接内存访问(DMA)
1. 什么是DMA 直接内存访问是一种硬件机制,它允许外围设备和主内存之间直接传输它们的I/O数据,而不需要系统处理器的参与.使用这种机制可以大大提高与设备通信的吞吐量. 2. DMA数据传输 有 ...
- UNIX网络编程——客户/服务器程序设计示范(一)
下面给出的是客户程序用于测试我们的服务器程序的各个变体. #include "unp.h" #define MAXN 16384 /* max # bytes to request ...
- Linux上程序调试的基石(1)--ptrace
引子: 1.在Linux系统中,进程状态除了我们所熟知的TASK_RUNNING,TASK_INTERRUPTIBLE,TASK_STOPPED等,还有一个TASK_TRACED.这表明这个进程处于什 ...
- C++_友元函数
1.为什么要引入友元函数:在实现类之间数据共享时,减少系统开销,提高效率 具体来说:为了使其他类的成员函数直接访问该类的私有变量 即:允许外面的类或函数去访问类的私有变量和保护 ...
- iOS中 本地通知/本地通知详解 韩俊强的博客
布局如下:(重点讲本地通知) iOS开发者交流QQ群: 446310206 每日更新关注:http://weibo.com/hanjunqiang 新浪微博 Notification是智能手机应用编 ...
- 安卓ListView的性能优化
在安卓APP中LIstView这个控件可以说基本上是个APP就会用到,但是关于ListView除了需要了解其最基本的用法外,作为一个要做出高性能APP的程序员还需了解一些关于LIstView控件性能优 ...
- java设计模式---调停者模式
中介者模式(Mediator):用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 通用类图: 举例:在一个公司里面 ...
- Deep Learning with Torch
原文地址:https://github.com/soumith/cvpr2015/blob/master/Deep%20Learning%20with%20Torch.ipynb Deep Learn ...