void OpenCVStudy::slot_ProcessImage()
{
Mat resImage;
int h = srcimg.rows;
int w = srcimg.cols;
//medianBlur(srcimg, resImage, 3);
// 3x3 中值滤波
Mat dst = srcimg.clone();
for (int row = 2; row < h - 2; row++) {
for (int col = 2; col < w - 2; col++) {
uchar pixels[9];
pixels[0] = srcimg.at<uchar>(row - 1, col - 1);
pixels[1] = srcimg.at<uchar>(row - 1, col);
pixels[2] = srcimg.at<uchar>(row - 1, col + 1);
pixels[3] = srcimg.at<uchar>(row, col - 1);
pixels[4] = srcimg.at<uchar>(row, col);
pixels[5] = srcimg.at<uchar>(row, col + 1);
pixels[6] = srcimg.at<uchar>(row + 1, col - 1);
pixels[7] = srcimg.at<uchar>(row + 1, col);
pixels[8] = srcimg.at<uchar>(row + 1, col + 1);
uchar medianPixel = bubbleSort(pixels, 9);
dst.at<uchar>(row, col) = medianPixel;
}
}
QImage disImage;
matToQImage(dst, disImage);
ui.labelResult->setPixmap(QPixmap::fromImage(disImage));
ui.labelResult->setScaledContents(true);
}
//冒泡排序
uchar OpenCVStudy::bubbleSort(uchar* arrayPixel,int sizeMask)
{
uchar temp;
for (int i = 0; i < sizeMask -1; i++)
{
for (int j = 0; j < sizeMask-1; j++)
{
if (arrayPixel[i]>arrayPixel[i+1])
{
temp = arrayPixel[i];
arrayPixel[i] = arrayPixel[i + 1];
arrayPixel[i + 1] = temp;
}
}
}
return arrayPixel[(sizeMask-1) / 2];
}