Opencv C++ Tutorial, Mat Roi, Region of interest


My favourite

  • Opencv tutorial people detection
  • Head people cascade download
  • Opencv tutorial optical flow
  • Opencv Video stabilization
  • Opencv car dataset download
  • Opencv tutorial Transparent mask
  • Opencv videowriter
  • Opencv FFMPEG
  • Opencv Canny edge and hough lines
  • Opencv ROI, Region of Interest

    Simple opencv C++ tutorial how to work with ROI. Code example about selecting the rectangle region of interest inside the image and cut or display part of the image from the bigger picture. There is nothing what is difficult about this. Only trick is about one line of code. 

    Rect RectangleToSelect(x,y,width,height)
    Mat source;
    Mat roiImage = source(RectangleToSelect);

    This is first post from this series. This simple opencv tutorials are all over the web. I would like to visualize all my steps through the code and //comment them. Each tutorial will contain small amount of step to keep reader focused.  First tutorial about mat resizeing is available under that link Mat Resize

    I am using Visual Studio 2015,  How to use Opencv 3.0.0 with Visual Studio can be found here install opencv visual studio 2015. In Visual studio 2015 is best options to use NUGET packages, Here is described how to install Opencv by NUGET. It is easy. Working under one minute after you find the NUGET packages console.. 

    Opencv select mat ROI tutorial example


    #include <Windows.h>
    #include "opencv2\highgui.hpp"
    #include "opencv2\imgproc.hpp"
    #include "opencv2/video/background_segm.hpp"
    #include "opencv2/video/tracking.hpp"

    using namespace cv;
    using namespace std;

    int main(int argc, const char** argv)
    //  Load the image from file
    Mat LoadedImage;
    // Just loaded image Lenna.png from project dir to LoadedImage Mat
    LoadedImage = imread("Lenna.png", IMREAD_COLOR);
    //I would like to visualize Mat step by step to see the result immediately.
    // Show what is in the Mat after load
    namedWindow("Step 1 image loaded", WINDOW_AUTOSIZE);
    imshow("Step 1 image loaded", LoadedImage);
    // Same the result from LoadedImage to Step1.JPG
    imwrite("Step1.JPG", LoadedImage);

    Opencv ROI

    // This construct Rectangle Rec start at x=100 y=100, width=200 and heigth=200
    Rect Rec(100, 100, 200, 200);

    //Draw the rectangle into LoadedImage
    //Parameters are (into Mat, Rec describe position where to draw rectangle
    // Scalar is Color, 1 is thickness, 8 is line type and 0 shift position
    rectangle(LoadedImage, Rec, Scalar(255), 1, 8, 0);

    // Show what rectangle
    namedWindow("Step 2 draw Rectangle", WINDOW_AUTOSIZE);
    imshow("Step 2 draw Rectangle", LoadedImage);

    Opencv ROI

    // Same the result from LoadedImage to Step2.JPG
    imwrite("Step2.JPG", LoadedImage);

    //Select area described by REC and result write to the Roi
    Mat Roi = LoadedImage(Rec);
    namedWindow("Step 3 Draw selected Roi", WINDOW_AUTOSIZE);
    imshow("Step 3 Draw selected Roi", Roi);
    // Same the result from LoadedImage to Step3.JPG
    imwrite("Step3.JPG", Roi);

    Opencv ROI

    // Put roi back into source image
    // If you want to show the detail and 
    // visualize with context of source image

    // This rectangle describe target, where you want to
    // put your roi into original image
    // !! width and height of where rect must match Roi size
    // Let put our Roi into origin
    Rect WhereRec(0, 0, Roi.cols, Roi.rows);
    // This copy Roi Image into loaded on position Where rec

    namedWindow("Step 4  Final result", WINDOW_AUTOSIZE);
    imshow("Step 4 Final result", LoadedImage);

    Opencv ROI

    // Same the result from LoadedImage to Step4.JPG
    imwrite("Step4.JPG", LoadedImage);


    No comments:

    Post a Comment


    Follow by Email

    Powered by Blogger.