Home > Software engineering >  OTSU threshold image video
OTSU threshold image video

Time:09-30

An error


Code:
# include & lt; stdio.h>
# include & lt; string>
# include "opencv2/highgui/highgui. HPP"
# include "opencv2/opencv. HPP"
using namespace std;
using namespace cv;
//big method function
Int OTSU (CV: : Mat srcImage)
{
Int nCols=srcImage. Cols;
Int nRows=srcImage. Rows;
Int threshold=0;
//initializes the statistical parameter
Int nSumPix [256].
Float nProDis [256].
For (int I=0; i <256; I++)
{
NSumPix [I]=0;
NProDis [I]=0;
}
//grayscale statistics the number of each pixel in the whole image in
For (int I=0; i {
For (int j=0; J & lt; NRows; J++)
{
NSumPix [(int) srcImage. At (I, j)] + +;
}
}
//calculate each probability distribution of image gray levels
For (int I=0; i <256; I++)
{
=(float) nSumPix nProDis [I] [I]/(nCols * nRows);
}
//traverse grayscale [0255], calculate the between-cluster variance threshold under
Float w0, w1, u0_temp u1_temp, u0, u1, delta_temp;
Double delta_max=0.0;
For (int I=0; i <256; I++)
{
//initialize the related parameter
W0=w1=u0_temp=u1_temp=u0=u1=delta_temp=0;
For (int j=0; J & lt; 256; J++)
{
//background section
If (j & lt;=I)
{
//I for segmentation threshold current, the first kind of total probability
W0 +=nProDis [j];
U0_temp +=j * nProDis [j];
}
//part prospects
The else
{
//I for segmentation threshold current, the first kind of total probability
W1 +=nProDis [j];
U1_temp +=j * nProDis [j];
}
}
//calculate all kinds of the average gray level
U0=u0_temp/w0;
U1=u1_temp/w1.
Delta_temp=(float) (w1 w0 * * pow ((u0 - u1), 2));
//in order to find the between-cluster variance threshold under
If (delta_temp & gt; Delta_max)
{
Delta_max=delta_temp;
Threshold=I;
}
}
Return threshold;
}
Int main ()
{
//read and judge
//CV: : Mat srcImage=CV: : imread (" 2. JPG ");
//if (! SrcImage. Data)
//return 1;
//to greyscale
VideoCapture cap (" walk. Avi ");
While (1) {
Mat frame;
Cap> Frame;
CV: : Mat srcGray;
CV: : cvtColor (frame, srcGray CV_RGB2GRAY);
CV: : imshow (" srcGray, "srcGray);
//call the OTSU binarization algorithm for threshold
if(! SrcGray. Empty ())
{
Int ostuThreshold=OTSU (srcGray);

STD: : cout & lt; //define the output image
CV: : Mat otsuResultImage=
CV: : Mat: : zeros (srcGray. Rows, srcGray cols, CV_8UC1);
//used threshold for binarization operation
For (int I=0; i {
For (int j=0; J & lt; SrcGray. Cols; J++)
{
//meet is greater than the threshold ostuThreshold buy 255
If (srcGray. At (I, j) & gt; OstuThreshold)
OtsuResultImage. At (I, j)=255;
The else
OtsuResultImage. At (I, j)=0.
}
}
CV: : imshow (" otsuResultImage, "otsuResultImage);
CV: : waitKey (0); }}
return 0;
}

CodePudding user response:

Collapsed in the pop-up dialog box, press the corresponding button to enter debugging press Alt + 7 key to view the Call Stack, namely "the Call Stack" from the inside to the following out of from the inner to outer function Call history, double-click a row to the cursor to the Call of the source code or assembly instruction, don't understand when double click on the next line, until we can read ,

CodePudding user response:

Please carefully check the image and the algorithm of parameter restrictions

CodePudding user response:

The problem of data types, first make clear what is your video format, the CV: first: imshow (" srcGray, "srcGray); The following all commented out, see and not the wrong

CodePudding user response:

Answer bar: translate into code block looked more comfortable
 # include & lt; stdio.h> 
# include & lt; string>
# include "opencv2/highgui/highgui. HPP"
# include "opencv2/opencv. HPP"
using namespace std;
using namespace cv;
//big method function
Int OTSU (CV: : Mat srcImage)
{
Int nCols=srcImage. Cols;
Int nRows=srcImage. Rows;
Int threshold=0;
//initializes the statistical parameter
Int nSumPix [256].
Float nProDis [256].
For (int I=0; i <256; I++)
{
NSumPix [I]=0;
NProDis [I]=0;
}
//grayscale statistics the number of each pixel in the whole image in
For (int I=0; i {
For (int j=0; J & lt; NRows; J++)
{
NSumPix [(int) srcImage. At (I, j)] + +;
}
}
//calculate each probability distribution of image gray levels
For (int I=0; i <256; I++)
{
=(float) nSumPix nProDis [I] [I]/(nCols * nRows);
}
//traverse grayscale [0255], calculate the between-cluster variance threshold under
Float w0, w1, u0_temp u1_temp, u0, u1, delta_temp;
Double delta_max=0.0;
For (int I=0; i <256; I++)
{
//initialize the related parameter
W0=w1=u0_temp=u1_temp=u0=u1=delta_temp=0;
For (int j=0; J & lt; 256; J++)
{
//background section
If (j & lt;=I)
{
//I for segmentation threshold current, the first kind of total probability
W0 +=nProDis [j];
U0_temp +=j * nProDis [j];
}
//part prospects
The else
{
//I for segmentation threshold current, the first kind of total probability
W1 +=nProDis [j];
U1_temp +=j * nProDis [j];
}
}
//calculate all kinds of the average gray level
U0=u0_temp/w0;
U1=u1_temp/w1.
Delta_temp=(float) (w1 w0 * * pow ((u0 - u1), 2));
//in order to find the between-cluster variance threshold under
If (delta_temp & gt; Delta_max)
{
Delta_max=delta_temp;
Threshold=I;
}
}
Return threshold;
}
Int main ()
{
//read and judge
//CV: : Mat srcImage=CV: : imread (" 2. JPG "); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related