如何将图像从左到右和从上到下拆分为M x N块,并将这些块存储在矢量opencv中 如何将一个垫物体/图像分割成m×n等分,并用OpenCV和C++ +将这些碎片从左到右,从上到下存储到垫向量中。
非常感谢这里有一个例子: 在循环中一个接一个地拍摄矩形,然后使用如何将图像从左到右和从上到下拆分为M x N块,并将这些块存储在矢量opencv中 如何将一个垫物体/图像分割成m×n等分,并用OpenCV和C++ +将这些碎片从左到右,从上到下存储到垫向量中。,c++,opencv,C++,Opencv,非常感谢这里有一个例子: 在循环中一个接一个地拍摄矩形,然后使用裁剪从全图中捕获选定的部分 请注意,全图可能无法完美分割,因此每行/每列中的一部分可能略大于其余部分 #include <opencv2/opencv.hpp> #include <highgui.h> #include <iostream> #include <string> #include <vector> //usage: app pic.ext M N int
裁剪
从全图中捕获选定的部分
请注意,全图可能无法完美分割,因此每行/每列中的一部分可能略大于其余部分
#include <opencv2/opencv.hpp>
#include <highgui.h>
#include <iostream>
#include <string>
#include <vector>
//usage: app pic.ext M N
int main(int argc, char* argv[])
{
int M=0,N=0;
M = std::stoi(argv[2]);
N = std::stoi(argv[3]);
cv::Mat img = cv::imread(argv[1]);
if (img.empty())
{
std::cout << "failed to open the image" << std::endl;
return -1;
}
std::vector<std::vector<cv::Mat>> result;
/* Regular size */
int crop_width = img.size().width / M;
int crop_height = img.size().height / N;
/* column / row first part size when part size is not evenly divided*/
int crop_width1 = img.size().width - (M-1)*crop_width;
int crop_height1 = img.size().height - (N-1)*crop_height;
int offset_x = 0;
int offset_y = 0;
for(int i = 0; i < M; i++) //rows
{
std::vector<cv::Mat> tmp;
offset_x = 0;
for(int j=0; j< N; j++) //columns
{
cv::Rect roi;
roi.x = offset_x;
roi.y = offset_y
roi.width = (j>0) ? crop_width : crop_width1;
roi.height = (i>0) ? crop_height : crop_height1;
offset_x += roi.width;
/* Crop the original image to the defined ROI */
cv::Mat crop = img(roi);
/* You can save part to a file: */
//cv::imwrite(std::to_string(i) + "x" + std::to_string(j)+".png", crop);
tmp.push_back(crop);
}
result.push_back(tmp);
offset_y += (i>0) crop_height : crop_height1;
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
//用法:app pic.ext M N
int main(int argc,char*argv[])
{
int M=0,N=0;
M=std::stoi(argv[2]);
N=std::stoi(argv[3]);
cv::Mat img=cv::imread(argv[1]);
if(img.empty())
{
标准:库特0)?作物高度:作物高度1;
偏移量x+=roi.width;
/*将原始图像裁剪到定义的ROI*/
cv::Mat crop=img(投资回报率);
/*您可以将零件保存到文件:*/
//cv::imwrite(std::to_string(i)+“x”+std::to_string(j)+“.png”,裁剪);
tmp.推回(作物);
}
结果:推回(tmp);
偏移量y+=(i>0)裁剪高度:裁剪高度1;
}
返回0;
}
与针对相反问题给出的解决方案相结合,这应该可以做到:
#include <opencv2/opencv.hpp>
std::vector<cv::Mat> splitImage( cv::Mat & image, int M, int N )
{
// All images should be the same size ...
int width = image.cols / M;
int height = image.rows / N;
// ... except for the Mth column and the Nth row
int width_last_column = width + ( image.cols % width );
int height_last_row = height + ( image.rows % height );
std::vector<cv::Mat> result;
for( int i = 0; i < N; ++i )
{
for( int j = 0; j < M; ++j )
{
// Compute the region to crop from
cv::Rect roi( width * j,
height * i,
( j == ( M - 1 ) ) ? width_last_column : width,
( i == ( N - 1 ) ) ? height_last_row : height );
result.push_back( image( roi ) );
}
}
return result;
}
int main()
{
cv::Mat image = cv::imread( "image.png" );
std::vector<cv::Mat> array_of_images = splitImage( image, 3, 2 );
for( int i = 0; i < array_of_images.size(); ++i )
{
cv::imshow( "Image " + std::to_string( i ), array_of_images[i] );
}
cv::waitKey( 0 );
}
#包括
std::vector splitImage(cv::Mat&image,int M,int N)
{
//所有图像的大小都应该相同。。。
整数宽度=image.cols/M;
int height=image.rows/N;
//…第m列和第n行除外
int width_last_column=width+(image.cols%width);
int height\u last\u row=高度+(image.rows%高度);
std::向量结果;
对于(int i=0;i
不确定“拆分”是什么意思,但您可以使用cv::Rect
保存cv::Mat
的“部分”,并可以将它们保存在向量中或任何您喜欢的地方。看见