[Image Processing] functions in C ++ using, rendering grayscale image histogram

1 Introduction

OpenCV using the read image is converted to grayscale image, the grayscale image histogram drawing. Click the histogram, the number of the console output pixel gray level.

2. Principle

(1) The principle is simple, the main advantage OpenCV read image, and converted to a grayscale image;

    srcImg = imread(" ......");    // “....” 代表图像地址
    if (srcImg.empty()) {
        return -1;
    }
    imshow(WINDOW_SRCIMG, srcImg);
    Mat grayImg;
    cvtColor(srcImg, grayImg, CV_BGR2GRAY);
    imshow(WINDOW_GRAYIMG, grayImg);

(2) use of a new class Mat canvas fixed resolution, the number of statistics in each gray level pixel histogram drawn on the canvas.

    int nRows = 700 , Here nCols = 600 ; 
    Mat g_dstImg (nRows, Here nCols, CV_8UC1, the Scalar :: All ( 0 )); // new canvas

    Canvas while avoiding the high gray level range beyond the canvas, employed in this program is scaled down method.

    int the MaxCount = arrayMax (grayLevel, 256 ); // looking at a certain number of gray levels up to the number of pixels 
    yscaleRate =   Double (nRows) / the MaxCount; // Y scaling 
    Double xscaleRate Here nCols = / 256 ; / / the X-scaling

3. Implementation Details

// histogram .cpp: Defines the entry point console application.
// Topic: draw a histogram of grayscale; please indicate the source: Chen_HW (https://www.cnblogs.com/chen-hw/p/11668119.html) 
// Env: VS2015 Debug + x64 + OpenCV3 .4.1
// a Date: 2018.11.22 by Chen_HW
#include " the stdafx.h " #include <opencv2 / opencv.hpp> #include <the iostream> #define WINDOW_SRCIMG "[] FIG source" #define WINDOW_GRAYIMG "] [grayscale " #define WINDOW_HIST" [] histogram " the using namespace STD; the using namespace CV; int arrayMax ( int g_arr [], int NUM); Mat drawHist(Mat &g_srcImg); void MouseHandle(int event, int x, int y, int flags, void *param); Point clickPoint,displayPoint; bool downFlag = false; Mat srcImg; double yscaleRate = 0; int main() { system("color 3f"); srcImg = imread(" ......"); // “....” 代表图像地址 if (srcImg.empty()) { return -1; } imshow(WINDOW_SRCIMG, srcImg); Mat grayImg; cvtColor(srcImg, grayImg, CV_BGR2GRAY); imshow(WINDOW_GRAYIMG, grayImg); Mat histImg = drawHist(grayImg); imshow(WINDOW_HIST, histImg); setMouseCallback(WINDOW_HIST, MouseHandle, (void *)&histImg);//(void *)&srcImg传递给void *param waitKey(); return 0; }
Mat drawHist(Mat
&g_srcImg) { int nRows = 700,nCols=600; Mat g_dstImg (nRows, Here nCols, CV_8UC1, the Scalar :: All ( 0 )); // new canvas int grayLevel [ 256 ] = { 0 }; for ( int I = 0 ; I <g_srcImg.rows; ++ I) { for ( int J = 0 ; J <g_srcImg.cols; ++ J) { grayLevel [( int ) g_srcImg.at <UCHAR> (I, J)] ++ ; } } int the MaxCount = arrayMax (grayLevel, 256 ) ; // looking for a number of gray levels in the largest number of pixels yscaleRate = Double(nRows) / the MaxCount; // Y scaling Double xscaleRate Here nCols = / 256 ; // X scaling int YAXIS [ 256 ], XAXIS [ 257 ]; for ( int m = 0 ; m < 256 ; m ++ ) { YAXIS [ m] = int (grayLevel [m] * yscaleRate); } // draw a histogram for ( int n-= 0 ; n-< 256 ; n-++ ) { IF (n-== 255 ) { XAXIS [n-+ . 1 ] = n-XAXIS []; } Rectangle (g_dstImg, Point (XAXIS [n-], YAXIS [n-]), Point (XAXIS [n- + . 1 ], 0 ), the Scalar ( 255 ), - . 1 ); // -1 represents filled rectangle; padding value represents not a rectangular frame, more convenient to observe the distribution of the number of gray scale pixel; // but because of the need to use a packed later, it is arranged to fill state } return g_dstImg; }
/ / NUM: number of array elements; g_max: returns the maximum int arrayMax ( int g_arr [], int intNUM) { = g_max 0 ; int I = 0 ; the while (I < NUM) { IF (g_arr [I]> = g_max) { g_max = g_arr [I]; I ++ ; } the else { I ++ ; } } return g_max; } // event mouse events (such as pressing the left mouse button, the left lift, mouse movement) x, y coordinates of the mouse void MouseHandle ( int event , int X, int Y, int the flags,void * param) { Mat & g_srcImg = * (Mat * ) (param); Mat g_tempImg = g_srcImg.clone (); int nCount = 0 ; // the column number of the white pixels int nLevelCount = 0 ; char text [ 20 is ] ; // store text information a float g_rate; // proportion of the total number of pixels of the pixel columns imshow (WINDOW_HIST, g_srcImg); Switch ( Event ) { Case EVENT_LBUTTONDOWN: clickPoint.x = X; clickPoint.y = Y; downFlag = true; break; default: break; } if (downFlag) { //displayPoint.x = clickPoint.x; for (int i = 0; i < g_srcImg.rows; ++i) { if (int(g_srcImg.at<uchar>(i, x)) == 255) { ++nCount; } } nLevelCount = (nCount / yscaleRate); g_rate = float(NCount / yscaleRate) / (srcImg.rows * srcImg.cols); // ratio is not calculated here precisely COUT << " The number of the gray scale pixel: " << << nLevelCount " ; total the ratio of the number of pixels: " << g_rate << endl; sprintf_s (text, " Rate:% F " , g_rate); // number of the gradation pixel ratio of the total number; putText (g_tempImg, text, ClickPoint, FONT_HERSHEY_PLAIN, . 1 , the Scalar ( 255 , 0 , 255 )); imshow (WINDOW_HIST, g_tempImg); downFlag = to false ; } }

4. Results

  The results shown below, a histogram for each click on the right, in the console will output the gray level pixel number, and the proportion of the total display pixel.

 

Guess you like

Origin www.cnblogs.com/chen-hw/p/11668119.html