OpenCV GUI basic operation, callback function, progress bar, cropping image, etc.

Source: Internet
Author: User

The code is reproduced, the source can not find, not affixed to the

Tool Bar Progress bar:

//ConvertColor.cpp : Defines the entry point of the console application. #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 Variablesco NST 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;} 

Effect:


Image Trimming Code:

ConvertColor.cpp: Defines the entry point of the console application. #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 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 Eventsstati c void Mouse_callback (int event, int x, int y, int, void *) {//When the left mouse button was pressed, record its position and save it in corner1if (event = = Event_lbuttondown) {Ldown = true;corner1.x = X;corner1.y = Y;cout << "Corner 1 reco rded at "<< corner1 << Endl;} When the left mouse button was released, record its position and save it in COrner2if (event = = Event_lbuttonup) {//Also check if user selection is bigger than-pixels (jut for fun!) if (ABS (x-corner1.x) > && abs (Y-CORNER1.Y) >) {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 Mouseif (Ldown = = True && Lup = False) {point P T;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 has been selectedif (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, C ORNER2.Y);//Make an image out of just the selected ROI and display it in a new Windowmat 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 Callba CK functionsetmousecallback ("cropping app", mouse_callback);//Exit by pressing ' Q ' while (char (Waitkey (1))! = ' Q ') {} return 0;}

Cutting effect:


OpenCV GUI basic operation, callback function, progress bar, cropping image, etc.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.