在openCV中使用cv::Rect函數進行圖像的分割。
cv::Rect
的參數爲( x,
y, width, height)
x,y:起始位置.
width,height:圖像的大小
具體如下情況:
情況1:從Mat格式中提取一個矩形區域
cv::Mat bigImage = cv::Mat::zeros(cv::Size(660,350));
cv::Mat smallImage = cv::Mat(bigImage, cv::Rect(0,0,110,70));
注意:該操作不會將bigImage中的數據信息copy到smallImage中,該操作僅僅是對smallImage的類型進行了一個相同的封裝。
情況2:數據複製情形:
cv::Mat smallImage = cv::Mat(bigImage, cv::Rect(0,0,110,70)).clone();
情況3:如果你需要提取圖像爲多個部分,或者將圖像切割爲很對個小塊時,採用二重循環,並且保存到一個vector中,使用時直接從vector調取。
PS:vector聲明爲全局變量
cv::Size smallSize(110,70);
std::vector<Mat> smallImages;
for (int y = 0; y < bigImage.rows; y += smallSize.height)
{
for (int x = 0; x < bigImage.cols; x += smallSize.width)
{
cv::Rect rect = cv::Rect(x,y, smallSize.width, smallSize.height);
smallImages.push_back(cv::Mat(bigImage, rect));
}
}