Share
Visit

Monday, December 7, 2015

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

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);
waitKey(1000);
// 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);
waitKey(1000);

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);
waitKey(1000);
// 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
Roi.copyTo(LoadedImage(WhereRec));

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

Opencv ROI


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


}




13 comments:

  1. I'm wondering.. what is the difference between the uses of / and \ at #include ""? thank you

    ReplyDelete
    Replies
    1. One should use uniform / and \ dependent of the OS one is using.
      In Windows: #include "opencv2\imgproc.hpp"
      In Linux: #include "opencv2/video/background_segm.hpp"

      Delete
  2. Thank you a lot. I posted very interesting research on OpenCv development here Feel free to check and ask me questions

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5.  เว็บไซด์ยอดนิยมอันดับหนึ่งของประเทศไทย มีกีฬาหลากหลายชนิดให้ร่วมสนุก เช่น  ฟุตบอล เทนนิส บาสเกตบอล ชกมวย เบสบอล เป็นต้น ร่วมลุ้นร่วมสนุกได้ทุกวัน เพียงเข้าไปที่
    จีคลับ
    จีคลับ

    ReplyDelete
  6. I like your current blog templete,would you like to give me a copy of your blog templete?.
    ไฮโล

    ReplyDelete
  7. ร้อยไหมปรับรูปหน้า ปรับรูปหน้าที่ไหไนดี ร้อยไหมที่ใดดี? กังนัมสถานพยาบาลเป็นคำตอบไม่ว่าจะเป็น ร้อยไหมหน้าเรียว เป็นวีไลน์แบบประเทศเกาหลีด้วยไหมก้าง ให้ท่านงามแบบประเทศเกาหลี ลดอายุ หน้าเด็ก ไม่เจ็บ ไม่บอบช้ำ ไม่บวม หน้าเป็นวีเชฟ ลดเหนียง เหนียงกระชับสมปรารถนาร้อยไหม
    ร้อยไหมหน้าเรียว
    ร้อยไหม วีเชฟ

    ReplyDelete

ad