2017-03-28 22 views
0
#include <fstream> 
#include <iostream> 
#include <string> 
#include <stdlib.h> 
#include <stdio.h> 
#include <iomanip> 
#include <stdio.h> 

#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 

using namespace cv; 
using namespace std; 

int main(int argc, char** argv) 
{ 
    ofstream fout("E:\\FYP\\image analysis\\ImageAnalysis\\ImageAnalysis\\Cropped_Image\\Details.txt"); 

    Mat image = imread("rsz_2rsz_2iron-man.png", -1); 

    //Declare global parameters 
    int  kernel = 3;      //to be input 
    int  levels = 16;     //to be input 
    int  gap = 5;      //to be input in mm 
    double depth_gap = 0.2;    //to be input in mm 
    int  feedrate = 300;     //to be input 
    int  background = 0; 
    int  width = image.cols; 
    int  height = image.rows; 
    double x_window = width/kernel; 
    double y_window = height/kernel; 
    int  x_anchor = 0; 
    int  y_anchor = 0; 
    double height_threshold = 0; 
    double width_threshold = 0; 

    int max_img_width = 570; //in mm 
    int max_img_height = 480; //in mm 

    cout << width << "x " << height << endl; 
    cout << "(Physical Dimension : " << x_window * gap << " mm x " << y_window *gap << " mm)" << endl << endl; 

    height_threshold = ceil((y_window *gap)/max_img_height); 
    width_threshold = ceil((x_window * gap)/max_img_width); 

    int width_panel = (x_window * gap)/width_threshold; 
    int height_panel = (y_window *gap)/height_threshold; 
    int no_panels = height_threshold * width_threshold; 

    cout << "No. of panels = " << height_threshold * width_threshold << endl; 
    cout << width_threshold << " (x-axis) x " << height_threshold << " (y-axis)" << endl << endl; 

    cout << "Size of each panels : " << (x_window * gap)/width_threshold << "mm (x-axis) by " << (y_window *gap)/height_threshold << "mm (y-axis)" << endl << endl; 

    //imshow("Original Image", image); 

    int image_width = width/width_threshold; 
    int image_height = height/height_threshold; 

    cout << image_height << " vs " << image_width << endl; 

    vector<Mat> smallImages; 

    for (int y = 0; y < height; y += image_height) 
    { 
     for (int x = 0; x < width; x += image_width) 
     { 
      Rect rect = Rect(x, y, image_width, image_height); 
      smallImages.push_back(Mat(image, rect)); 
     } 
    } 


    //For displaying and saving cropped image 
    for (int panel = 0; panel < no_panels; panel += 1) 
    { 
     string num_panel = to_string(panel); 
     string filetype = ".png"; 
     string filename = "Image " + num_panel + filetype; 
     //cout << num_panel << endl; 
     //imshow(filename, smallImages[panel]); 
     //waitKey(0); 
     string location = "E:\\FYP\\image analysis\\ImageAnalysis\\ImageAnalysis\\Cropped_Image\\" + filename; 
     imwrite(location, smallImages[panel]); 
    } 

    fout << "Kernel = " << kernel << endl; 
    fout << "Levels = " << levels << endl; 
    fout << "Gap = " << gap << endl; 
    fout << "Max Height = " << max_img_width << endl; 
    fout << "Max Width = " << max_img_height << endl; 
    fout << "Number of Panels = " << height_threshold * width_threshold << endl; 
    fout << "Size of each panels : " << (x_window * gap)/width_threshold << "mm (x-axis) by " << (y_window *gap)/height_threshold << "mm (y-axis)" << endl << endl; 

    waitKey(0); 
    system("PAUSE"); 
    //return 0; 
} 

このプログラムは、指定された最大幅と高さと他のいくつかの特性に基づいて、大きな画像を複数の画像に分割することを目的としています。OpenCVアサーションが失敗しましたか?

異なるカーネルサイズでプログラムを実行すると、アサーションが失敗したり、場合によってはOKになることがありました。

エラー:あなたがイメージの外にある四角形を取得している

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat, file C:\builds\master_PackSlave-win64-vc12-shared\opencv\modules\core\src\matrix.cpp, line 508 
OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat, file C:\builds\master_PackSlave-win64-vc12-shared\opencv\modules\core\src\matrix.cpp, line 508 
OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat, file C:\builds\master_PackSlave-win64-vc12-shared\opencv\modules\core\src\matrix.cpp, line 508 
OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat, file C:\builds\master_PackSlave-win64-vc12-shared\opencv\modules\core\src\matrix.cpp, line 508 
OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat, file C:\builds\master_PackSlave-win64-vc12-shared\opencv\modules\core\src\matrix.cpp, line 508 

答えて

0

。これは、ループの条件が間違っているためです(for)。用途:

for (int y = 0; y <= (height - image_height); y += image_height) 
{ 
    for (int x = 0; x <= (width - image_width); x += image_width) 
    { 
      Rect rect = Rect(x, y, image_width, image_height); 
      smallImages.push_back(Mat(image, rect)); 
    } 
} 

あなたのオリジナルの条件は、それぞれ、heightwidthimage_heightimage_widthの倍数である場合にのみ、正しいです。

+1

ありがとうございました。それは働いている.. :) – Hasics

関連する問題