这里我有一个代码(算法)使用openCV来改善绿板(我们在学校使用)的图像,过滤噪音并改变黄色和黑色的极性。
我需要在opencv4android上实现使用相机进行实时渲染。
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);