void OpenCVStudy::slot_ProcessImage()
{
Mat ResImg;
//转灰度图
//cvtColor(srcimg, ResImg, COLOR_BGR2GRAY);
int h = srcimg.rows;
int w = srcimg.cols;
// 3x3 均值滤波
Mat dst = srcimg.clone();
for (int row = 1; row < h - 1; row++) {
for (int col = 1; col < w - 1; col++) {
Vec3b pixel1 = srcimg.at<Vec3b>(row - 1, col - 1);
Vec3b pixel2 = srcimg.at<Vec3b>(row - 1, col);
Vec3b pixel3 = srcimg.at<Vec3b>(row - 1, col + 1);
Vec3b pixel4 = srcimg.at<Vec3b>(row, col - 1);
Vec3b pixel5 = srcimg.at<Vec3b>(row, col);
Vec3b pixel6 = srcimg.at<Vec3b>(row, col + 1);
Vec3b pixel7 = srcimg.at<Vec3b>(row + 1, col - 1);
Vec3b pixel8 = srcimg.at<Vec3b>(row + 1, col);
Vec3b pixel9 = srcimg.at<Vec3b>(row + 1, col + 1);
int channelR = pixel1[0] + pixel2[0] + pixel3[0] + pixel4[0] + pixel5[0] + pixel6[0] + pixel7[0] + pixel8[0] + pixel9[0];
int channelG = pixel1[1] + pixel2[1] + pixel3[1] + pixel4[1] + pixel5[1] + pixel6[1] + pixel7[1] + pixel8[1] + pixel9[1];
int channelB = pixel1[2] + pixel2[2] + pixel3[2] + pixel4[2] + pixel5[2] + pixel6[2] + pixel7[2] + pixel8[2] + pixel9[2];
dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(channelR / 9);
dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(channelG / 9);
dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(channelB / 9);
}
}
QImage disImage;
matToQImage(dst, disImage);
ui.labelResult->setPixmap(QPixmap::fromImage(disImage));
ui.labelResult->setScaledContents(true);
}
貌似转换通道除了问题!
关注公众号,更多知识内容