进度条演示
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Mat img;
int threshval = 100;
#define FILE_PATH_TEST "E:\\sunwork\\OpenCVTest\\TestImage\\stuff.jpg"
void on_trackbar(int, void*)
{
Mat bw = threshval < 128 ? (img < threshval) : (img > threshval);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours( bw, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
//namedWindow( "Image11", 1 );
// imshow( "Image11", contours );
Mat dst = Mat::zeros(img.size(), CV_8UC3);
if( !contours.empty() && !hierarchy.empty() )
{
// iterate through all the top-level contours,
// draw each connected component with its own random color
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
{
//Scalar color( (rand()&255), (rand()&255), (rand()&255) );//generate rand color
Scalar color( (255), (255), (255) );//generate rand color
drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
}
}
imshow( "Connected Components", dst );
}
void help()
{
cout << "\n This program demonstrates connected components and use of the trackbar\n"
"Usage: \n"
" ./connected_components <image(stuff.jpg as default)>\n"
"The image is converted to grayscale and displayed, another image has a trackbar\n"
"that controls thresholding and thereby the extracted contours which are drawn in color\n";
}
int main( int argc, const char** argv )
{
help();
string inputImage ="stuff.jpg";
img = imread(FILE_PATH_TEST, 0);//Read the gray image
if(img.empty())
{
cout << "Could not read input image file: " << inputImage << endl;
return -1;
}
namedWindow( "Image", 1 );
imshow( "Image", img );
namedWindow( "Connected Components", 1 );
createTrackbar( "Threshold", "Connected Components", &threshval, 255, on_trackbar );
on_trackbar(threshval, 0);
waitKey(60000);
return 0;
}
进度条对图像像素点操作演示
//-----------------------------------【程序说明】----------------------------------------------
// 程序名称::创建Trackbar&图像对比度、亮度值调整
// VS2010 OpenCV:2.3.1
// 2017年1月17 日 Use by sunlinju
//------------------------------------------------------------------------------------------------
#include <opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include"opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
static void ContrastAndBright(int, void *);
#define FILE_PATH_TEST "E:\\sunwork\\OpenCVTest\\TestImage\\TEST.jpg"
int g_nContrastValue;
int g_nBrightValue;
Mat g_srcImage,g_dstImage;
int main( )
{
//set initial value
g_nContrastValue=80;
g_nBrightValue=80;
g_srcImage= imread( FILE_PATH_TEST); //imread the image
if(!g_srcImage.data ) { printf("Imread image wrong!\n"); return false; }
g_dstImage= Mat::zeros( g_srcImage.size(), g_srcImage.type() );
namedWindow("WindowsName", 1);
createTrackbar("Contrast", "WindowsName",&g_nContrastValue,300,ContrastAndBright );
createTrackbar("Bright","WindowsName",&g_nBrightValue,200,ContrastAndBright );
//recall the function
ContrastAndBright(g_nContrastValue,0);
ContrastAndBright(g_nBrightValue,0);
//printf some help information
cout<<endl<<"\t[1]you can set the contrast and brightness;\n\n"
<<"\t[2]Press q,quit the programe;\n"
<<"\n\n\t\t\t\tby sun";
//Press q for quite
while(char(waitKey(1)) != 'q') {}
return 0;
}
//-----------------------------ContrastAndBright( )------------------------------------
// Descripition:Convert the Contrast and brightness
//-------------------------------------------------------------------------------------
static void ContrastAndBright(int, void *)
{
namedWindow("【Original】", 1);
//do g_dstImage(i,j) =a*g_srcImage(i,j) + b
for(int y = 0; y < g_srcImage.rows; y++ )
{
for(int x = 0; x < g_srcImage.cols; x++ )
{
for(int c = 0; c < 3; c++ )
{
g_dstImage.at<Vec3b>(y,x)[c]= saturate_cast<uchar>( (g_nContrastValue*0.01)*(g_srcImage.at<Vec3b>(y,x)[c] ) + g_nBrightValue );
}
}
}
//y是像素所在的行, x是像素所在的列, c是R、G、B(对应0、1、2)其中之一
//运算结果可能超出像素取值范围(溢出),还可能是非整数(如果是浮点数的话),所以我们要用saturate_cast对//结果进行转换,以确保它为有效值
//a也就是对比度,一般为了观察的效果,取值为0.0到3.0的浮点值,但是我们的轨迹条一般取值都会整数,所以在这//里我们可以,将其代表对比度值的nContrastValue参数设为0到300之间的整型,在最后的式子中乘以一个0.01,//这样就可以完成轨迹条中300个不同取值的变化
imshow("【Original】", g_srcImage);
imshow("WindowsName", g_dstImage);
}