MFC+Opencv3 Camera Read+Gray Histogram+Frame Number Comparison

Keywords: Windows

1. Create a dialog-based MFC program under vs.

The layout is as follows

2. Initialize

Add the following code to the OnInitDialog() function in the C***Dlg class

                        

        CWnd  *pWnd1 = GetDlgItem(IDC_PIC1);     //Gets the window class that displays the camera picture (parameter is control ID)
        CWnd  *pWnd2 = GetDlgItem(IDC_PIC2);      //Get the window class that displays the histogram
        pWnd1->GetClientRect(&rect1);            //GetClientRect to get the coordinate size of the control itself
	pWnd2->GetClientRect(&rect2);            
	namedWindow("gray", WINDOW_AUTOSIZE);        //Set the window name for grayscale display  
	namedWindow("src", WINDOW_AUTOSIZE);         //Set screen display window name
	HWND hWndl = (HWND)cvGetWindowHandle("src");    //hWnd represents window handle, get window handle
	HWND hWnd2 = (HWND)cvGetWindowHandle("gray");
	HWND hParent1 = ::GetParent(hWndl);             //GetParent function gets parent window handle of child window
	HWND hParent2 = ::GetParent(hWnd2);
	::SetParent(hWndl,pWnd1->m_hWnd);                //Sets the parent class of a window, that is, binds two windows to two controls
	::SetParent(hWnd2, pWnd2->m_hWnd);
	capture.open(0);                              //Turn on the default camera
	capture >> frame;                               //Get the first frame
	resize(frame, frame, Size(rect1.Width(), rect1.Height()));      //Size to control
	::ShowWindow(hParent1, SW_HIDE);   //Hide the parent window of the two windows. If you do not hide it, the two windows will be shown. The reason for this needs to be explained by God.
	::ShowWindow(hParent2, SW_HIDE);
	imshow("src", frame);          //Show window in dialog
	cvtColor(frame, frame,COLOR_RGB2GRAY);      //Convert to Grayscale
	grayHist(frame);                  //Draw grayscale and display
Some of the above variables are then declared in the class, all public

static BOOL isClose; //whether the program ends
VideoCapture capture; //Video class
Mat frame, next;//Number of frames for display
CRect rect1,rect2; //Control size
int Max;//Get the maximum change of two grayscale frames
void grayHist(Mat src); //Grayscale display
void frameVar(Mat src,Mat next); //Get the maximum change of two grayscale frames
CString m_diff;//This variable is bound to static text to show the maximum value of change

afx_msg void OnBnClickedButton1(); //Button message
Afx_Message for the MSG void OnClose (); //close button to end the loop

Button message implementation OnBnClickedButton1()

while (!isClose)          //isClose is used to determine if it ends
	{
		capture >> next;      //Get each frame
		resize(next, next, Size(rect1.Width(), rect1.Height()));  //Zoom the picture to the same size as the control
		frameVar(frame, next);            //Get the maximum difference of gray level changes between two frames
		next.copyTo(frame);                //Frame represents the previous frame, next frame
		m_diff.Format(_T("%d"), Max);          //Enter value into static text text
		UpdateData(FALSE);                    //Update to pass variable values into the control
		imshow("src", frame);                 //Show Picture to Text
		cvtColor(frame, frame, COLOR_RGB2GRAY);        //Convert to Grayscale
		grayHist(frame);               //Draw a grayscale image and display it to the control
		waitKey(30);                 //Every 30ms frame
	}
Close button message implementation OnClose()

isClose=TRUE; //End loop

grayHist(Mat src) drawing of grayscale histogram

	int arryNum = 1;
	const int channels[] = { 0 };
	int dims = 1;
	const int histSize = 256;
	float hranges[] = { 0,256 };
	const float *ranges[] = { hranges };
	Mat dstHist;
	calcHist(&src, arryNum, channels, Mat(), dstHist, dims, &histSize, ranges);
	double maxValue = 0;
	minMaxLoc(dstHist, NULL, &maxValue, 0, 0);
	Mat dstImage(Size(histSize+10, histSize), CV_8UC1, Scalar(255));
	for (int i = 0; i < histSize; ++i)
	{
		int realValue = saturate_cast<int>(dstHist.at<float>(i, 0)*0.9*histSize / maxValue);
		line(dstImage, Point(i * 1+5, histSize - 10), Point(i * 1+5, histSize - 10 - realValue), Scalar(88, 100, 52), 1, 4);
	}
	resize(dstImage, dstImage, Size(rect2.Width(), rect2.Height()));
	imshow("gray", dstImage); 
Evaluate difference frameVar(Mat src,Mat next)
	Max = 0;
	Mat diff = Mat(src.rows, src.cols, CV_8UC1);
	int width = src.cols;
	int height = src.rows;
	for (int i = 0; i < height; ++i)
	{
		uchar *data1 = src.ptr<uchar>(i);
		uchar *data2 = next.ptr<uchar>(i);
		uchar *data3 = diff.ptr<uchar>(i);
		for (int j = 0; j < width; ++j)
		{
			data3[j] = fabs(data1[j] -data2[j]);
		}
	}
	for (int i = 0; i < height; ++i)
	{
		uchar *data = diff.ptr<uchar>(i);
		for (int j = 0; j < width; ++j)
		{
			if ((int)data[j] > Max)
				Max = data[j];
		}
	}

Posted by cjosephson on Wed, 08 Jul 2020 08:28:34 -0700