遍历区域并在 OpenCV 中获取平均像素值?
Iterate over regions and take mean pixel value in OpenCV?
所以我主要是尝试通过调整初始图像的大小来划分灰度图像(在本例中为 32x32)。
一旦 "regions" 被划分,我需要取每个像素的平均像素值,然后将 1、0 或 X 添加到字符串中。例如:"Region (3, 19) has a mean value of 21 so that's a 1"。
我想我已经掌握了大部分逻辑,但理论上,输出不应该以 1、0 和 X 的形式重新创建图像吗?我觉得我的数学在 for 循环上可能是错误的?请记住,我要做的就是将图像分解为 MxN table 或网格,并取每个网格区域的 0 通道值的平均值。
这是我的代码:
Mat image = imread("blackdot.jpg", IMREAD_GRAYSCALE); //Pass in image
imshow("Gray scaled image", image); //imshow("Passed in gray scale image", image);
Mat resizedImage; // New Mat for saving the blown up image
resize(image, resizedImage, Size(3200, 3200)); // Blow up image so it's divisible by 32 using passed in image
string bitStream; // Ternary bitstream
for (int y = 0; y<resizedImage.cols - 32; y += 32) {
for (int x = 0; x<resizedImage.rows - 32; x += 32) {
// get the average for the whole 32x32 block
Rect roi(x, y, 32, 32);
Scalar mean, dev;
meanStdDev(resizedImage(roi), mean, dev); // mean[0] is the mean of the first channel, gray scale value;
if (mean[0] >= 0 && mean[0] <= 25) {
if ((counter % 3200) == 2900) {
bitStream += "1\n";
counter = 0;
}
else {
bitStream += "1";
}
else if (mean[0] >= 77 && mean[0] <= 153) {
if ((counter % 3200) == 2900) {
bitStream += "X\n";
counter = 0;
}
else {
bitStream += "X";
}
else {
if ((counter % 3200) == 2900) {
bitStream += "0\n";
counter = 0;
}
else {
bitStream += "0";
}
}
}
cout << bitStream;
blackdot.jpg
代码和逻辑看起来不错,对于每个像素分布,将相应的字符添加到比特流中,并对行中的所有像素和图像中的每一列重复该操作。
向比特流添加字符时,尝试在到达换行符时(即完成一行时)将 \n
附加到比特流,以在比特流中说明图片。这相当于您的代码中的这个小调整:
for (int y = 0; y<resizedImage.cols - 32; y += 32) {
for (int x = 0; x<resizedImage.rows - 32; x += 32) {
// get the average for the whole 32x32 block
Rect roi(x, y, 32, 32);
Scalar mean, dev;
meanStdDev(resizedImage(roi), mean, dev); // mean[0] is the mean of the first channel, gray scale value;
if (mean[0] >= 0 && mean[0] <= 25) {
bitStream += "1";
}
else if (mean[0] >= 77 && mean[0] <= 153) {
bitStream += "X";
}
else {
bitStream += "0";
}
}
//after each row has been checked, add newline
bitStream += '\n';
}
注意:输出 window 可能需要最大化才能看到正确的结果。
所以我主要是尝试通过调整初始图像的大小来划分灰度图像(在本例中为 32x32)。
一旦 "regions" 被划分,我需要取每个像素的平均像素值,然后将 1、0 或 X 添加到字符串中。例如:"Region (3, 19) has a mean value of 21 so that's a 1"。
我想我已经掌握了大部分逻辑,但理论上,输出不应该以 1、0 和 X 的形式重新创建图像吗?我觉得我的数学在 for 循环上可能是错误的?请记住,我要做的就是将图像分解为 MxN table 或网格,并取每个网格区域的 0 通道值的平均值。
这是我的代码:
Mat image = imread("blackdot.jpg", IMREAD_GRAYSCALE); //Pass in image
imshow("Gray scaled image", image); //imshow("Passed in gray scale image", image);
Mat resizedImage; // New Mat for saving the blown up image
resize(image, resizedImage, Size(3200, 3200)); // Blow up image so it's divisible by 32 using passed in image
string bitStream; // Ternary bitstream
for (int y = 0; y<resizedImage.cols - 32; y += 32) {
for (int x = 0; x<resizedImage.rows - 32; x += 32) {
// get the average for the whole 32x32 block
Rect roi(x, y, 32, 32);
Scalar mean, dev;
meanStdDev(resizedImage(roi), mean, dev); // mean[0] is the mean of the first channel, gray scale value;
if (mean[0] >= 0 && mean[0] <= 25) {
if ((counter % 3200) == 2900) {
bitStream += "1\n";
counter = 0;
}
else {
bitStream += "1";
}
else if (mean[0] >= 77 && mean[0] <= 153) {
if ((counter % 3200) == 2900) {
bitStream += "X\n";
counter = 0;
}
else {
bitStream += "X";
}
else {
if ((counter % 3200) == 2900) {
bitStream += "0\n";
counter = 0;
}
else {
bitStream += "0";
}
}
}
cout << bitStream;
blackdot.jpg
代码和逻辑看起来不错,对于每个像素分布,将相应的字符添加到比特流中,并对行中的所有像素和图像中的每一列重复该操作。
向比特流添加字符时,尝试在到达换行符时(即完成一行时)将 \n
附加到比特流,以在比特流中说明图片。这相当于您的代码中的这个小调整:
for (int y = 0; y<resizedImage.cols - 32; y += 32) {
for (int x = 0; x<resizedImage.rows - 32; x += 32) {
// get the average for the whole 32x32 block
Rect roi(x, y, 32, 32);
Scalar mean, dev;
meanStdDev(resizedImage(roi), mean, dev); // mean[0] is the mean of the first channel, gray scale value;
if (mean[0] >= 0 && mean[0] <= 25) {
bitStream += "1";
}
else if (mean[0] >= 77 && mean[0] <= 153) {
bitStream += "X";
}
else {
bitStream += "0";
}
}
//after each row has been checked, add newline
bitStream += '\n';
}
注意:输出 window 可能需要最大化才能看到正确的结果。