OpenCV the mean value of the ROI region RGB, to find the variance, the ROI region color is clearly differentiated

Source: Internet
Author: User
Tags pow
#include <iostream>
#include <math.h>

#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#define REC_W_H 30
using namespace Std;
int main ()
{

Iplimage *res, *DST;
Iplimage *dst1, *dst2, *dst3;
Cvrect rect;
Cvrect rect1, Rect2, rect3;
Rect.x = Rect.y =, Rect.width = rec_w_h, rect.height = rec_w_h;//roi0 coordinates and size
rect1.x = Rect1.y = +, Rect1.width = rec_w_h, rect1.height = rec_w_h;//roi1 coordinates and size
rect2.x = 1230, rect2.y = 510, Rect2.width = rec_w_h, rect2.height = rec_w_h;//roi2 coordinates and size
rect3.x = 1230, RECT3.Y = 560, Rect3.width = rec_w_h, rect3.height = rec_w_h;//roi3 coordinates and size
res = Cvloadimage ("d:/project/tt/1.bmp");//Load a picture
DST = Cvcreateimage (Cvsize (Rec_w_h, Rec_w_h), 8, 3);//Create Image Space
Dst1 = Cvcreateimage (Cvsize (Rec_w_h, Rec_w_h), 8, 3);//Create Image Space 1
Dst2 = Cvcreateimage (Cvsize (Rec_w_h, Rec_w_h), 8, 3);//Create Image Space 2
Dst3 = Cvcreateimage (Cvsize (Rec_w_h, Rec_w_h), 8, 3);//Create Image Space 3
Create window
Cvnamedwindow ("res", cv_window_autosize);
Cvnamedwindow ("DST", cv_window_autosize);
Cvnamedwindow ("Dst1", cv_window_autosize);
Cvnamedwindow ("Dst2", cv_window_autosize);
Cvnamedwindow ("Dst3", cv_window_autosize);

Set ROI Area
Cvsetimageroi (res, rect);
Extract ROI
Cvcopy (res, DST);


Cvsetimageroi (res, rect1);
Cvcopy (res, dst1);


Cvsetimageroi (res, RECT2);
Cvcopy (res, DST2);


Cvsetimageroi (res, RECT3);
Cvcopy (res, DST3);

Cancel Settings
Cvresetimageroi (RES);

Display image
Cvshowimage ("res", res);
Cvshowimage ("DST", DST);

Cvshowimage ("Dst1", dst1);
Cvshowimage ("Dst2", dst2);
Cvshowimage ("Dst3", DST3);


To extract the average value of the image ROI region RGB, we found a cvavg function:
Cvscalar cvavg (const cvarr* arr, const cvarr* mask=null);
Cvscalar avgchannels = Cvavg (DST);
Double AVGB = avgchannels.val[0];
Double Avgg = avgchannels.val[1];
Double AVGR = mean value of Avgchannels.val[2];//roi

Cvscalar avgChannels1 = Cvavg (Dst1);
Double avgB1 = avgchannels1.val[0];
Double avgG1 = avgchannels1.val[1];
Double avgR1 = mean value of Avgchannels1.val[2];//roi 1

Cvscalar avgChannels2 = Cvavg (DST2);
Double avgB2 = avgchannels2.val[0];
Double avgG2 = avgchannels2.val[1];
Double avgR2 = mean value of Avgchannels2.val[2];//roi 2

Cvscalar AVGCHANNELS3 = Cvavg (DST3);
Double avgB3 = avgchannels3.val[0];
Double avgG3 = avgchannels3.val[1];
Double avgR3 = mean value of Avgchannels3.val[2];//roi 3


DOUBLE ret = sqrt (Pow ((AVGB-AVGB2), 2) + POW ((AVGG-AVGG2), 2) + POW ((AVGR-AVGR2), 2));
Double ret1 = sqrt (Pow ((AVGB1-AVGB2), 2) + POW ((AVGG1-AVGG2), 2) + POW ((AVGR1-AVGR2), 2));
Double Ret2 = sqrt (Pow ((AVGB-AVGB3), 2) + POW ((AVGG-AVGG3), 2) + POW ((AVGR-AVGR3), 2));
Double ret3 = sqrt (Pow ((AVGB1-AVGB3), 2) + POW ((AVGG1-AVGG3), 2) + POW ((AVGR1-AVGR3), 2));

cout << "Result:" << ret << Endl;
cout << "RESULT1:" << ret1 << Endl;
cout << "RESULT2:" << ret2 << Endl;
cout << "RESULT3:" << ret3 << endl;//console Print Variance value

CV_BGR2HSV ();

Cvwaitkey (0);

Cvdestroywindow ("res");
Cvdestroywindow ("DST");

Cvdestroywindow ("Dst1");
Cvdestroywindow ("Dst2");
Cvdestroywindow ("Dst3");

Cvreleaseimage (&res);
Cvreleaseimage (&AMP;DST);

Cvreleaseimage (&AMP;DST1);
Cvreleaseimage (&AMP;DST2);
Cvreleaseimage (&AMP;DST3);

return 0;

}

Above content, own handwriting, reprint please indicate source. Thank you..

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.