我是新的 opencv 。我写了一个删除背景。
我的输入图像
我将我的程序编码为以下步骤:
- 计算平均像素
//define roi of image
cv::Rect roi(0, 0, 20 , 20 );
//copies input image in roi
cv::Mat image_roi = imgGray( roi );
//imshow("roi", image_roi);
//computes mean over roi
cv::Scalar avgPixelIntensity = cv::mean( image_roi );
//prints out only .val[0] since image was grayscale
cout << "Pixel intensity over ROI = " << avgPixelIntensity.val[0] << endl;
- 根据平均像素值创建新的 Mat 图像:
//create new mat image base on avgPixelIntensity
cv::Mat areaSampleArv(imgGray.rows, imgGray.cols,imgGray.type(),avgPixelIntensity.val[0]);
imshow("areaSampleArv", areaSampleArv);
- 反转图像:
void image_invert(Mat& image){
int height, width, step, channels;
uchar *data;
height = image.cols;
width = image.rows;
step = (int)image.step;
channels = image.channels();
data = (uchar *)image.data;
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
for(int k = 0; k < channels; k++){
data[i*step + j*channels + k] = 255 - data[i*step + j*channels + k];
}
}
}
//imwrite("/Users/thuydungle/Desktop/1234/inverted.png", image);
imshow("inverted", image);}
我的图像反转结果:
- 使用原始图像添加倒置图像:
Mat dst;
dst = areaSampleArv + im0;
imshow("dst", dst);
我的任何图像结果:
看起来很糟糕,我可以使用阈值来提取数字吗?
那么,你能告诉我如何解决吗?
感谢 !