Android Threshold image processing
Here I have a code (algorithm) using openCV for improving the image of a green board (that we use in schools) filtering the noise and changing the polarity in yellow and black.
and I need to implement on opencv4android for real-time rendering using the camera.
Orginal:
Threshold:
int x,y; // variables to be used for thresholding
int a=0; // variables to be used in loop
int count=0; // variables to be used in loop
//Step: 1 SImple brightness and contrast control using alpha and beta variables
for( int y = 0; y < image.rows; y++ )
{ for( int x = 0; x < image.cols; x++ )
{ for( int c = 0; c < 3; c++ )
{
image.at<Vec3b>(y,x)[c] saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
}
}
}
// Step: 3 calculating the threshold value using gray scale image
for( x = 0; x < gray_image.rows; x++ )
{
for ( y = 0; y < gray_image.cols; y++ )
{
a=a+gray_image.at<uchar>(x,y);
count=count+1;
}
}
int thresh=0;;
thresh=a/count;
printf("%d",thresh); // Calculated the threshold of our image
// Step:3 Applying the thresholding operation to our image
for( x = 0; x < image.rows; x++ )
{
for ( y = 0; y < image.cols; y++ )
{
if(gray_image.at<uchar>(x,y)<thresh)
{image.at<cv::Vec3b>(x,y)[0] = 235; // 0 element is for B component , 1 is for Green component, 2 is for Red component as in openCV BGR is the standard
image.at<cv::Vec3b>(x,y)[1] =206;
image.at<cv::Vec3b>(x,y)[2] = 135; } // below threshold value pixels are assigned whatever colour you decide
else
{image.at<cv::Vec3b>(x,y)[0] = 0;
image.at<cv::Vec3b>(x,y)[1] = 0;
image.at<cv::Vec3b>(x,y)[2] = 0; } // above threshold value pixels are assigned this colour
}
}
cv::namedWindow("Final threshold");
cv::imshow("Final threshold",image);
imwrite("F:Threshold image.jpg",image); // saved the image to F drive with name Threshold Image
waitKey(100);
链接地址: http://www.djcxy.com/p/79634.html
上一篇: 从照片中找到广场中心
下一篇: Android阈值图像处理