#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/photo.hpp>
#include <vector>
using namespace cv;
using namespace std;
int main() {
Mat img = imread("example.jpg"); // загружаем изображение
int block_size = 50;
int num_blocks_x = img.cols / block_size;
int num_blocks_y = img.rows / block_size;
int num_blocks = num_blocks_x * num_blocks_y;
vector<Mat> block;
for (int i = 0; i < num_blocks_y; i++)
{
for (int j = 0; j < num_blocks_x; j++)
{
block= img(Rect(j * block_size, i * block_size, block_size, block_size));
}
}
vector <double> variances;
for (int i = 0; i < num_blocks; i++) {
Scalar mean, stddev;
meanStdDev(block[i], mean, stddev);
variances[i] = stddev.val[0];
}
int min_indices[5];
for (int i = 0; i < 5; i++) {
double min_val = 0;
int min_idx = -1;
for (int j = 0; j < num_blocks; j++) {
if (variances[j] < min_val) {
min_val = variances[j];
min_idx = j;
}
}
min_indices[i] = min_idx;
variances[min_idx] = DBL_MAX;
}
for (int i = 0; i < 5; i++) {
Mat smoothed_block;
blur(block[min_indices[i]], smoothed_block, Size(3, 3));
smoothed_block.copyTo(img(Rect((min_indices[i] % (img.cols / block_size)) * block_size,
(min_indices[i] / (img.cols / block_size)) * block_size,
block_size, block_size)));
}
imshow("Smoothed Image", img);
}
Почему выбивает исключение на моменте деления изображения на равные блоки ?