opencv image croping function not working

I'm trying to crop an image using opencv ROI method. I'm able to draw ROI over image but i cant crop the image.When i try to crop, the given image itself is showing on the display app. My code is given below. Any help will be appreciated.

 

 

#include <Arduino.h>
#include <Camera.h>
#include <opencv.hpp>
#include <DisplayApp.h>
using namespace cv;

#define IMAGE_HW 320
#define IMAGE_VW 240

Camera camera(IMAGE_HW, IMAGE_VW);
static DisplayApp display_app;
uint8_t gray_buf[3 * IMAGE_HW * IMAGE_VW]__attribute((section("NC_BSS"),aligned(32)));

void setup() {
     camera.begin(); 
}

void loop() {
     Mat img_raw(IMAGE_VW, IMAGE_HW, CV_8UC2, camera.getImageAdr());//reading camera image 
     Mat src(IMAGE_VW, IMAGE_HW, CV_8UC2, gray_buf);
     cvtColor(img_raw, src, COLOR_YUV2GRAY_YUYV);//converting to gray
     Rect Rec(0, 0, 50, 50);//defining roi
     rectangle(src, Rec, Scalar(255), 1, 8, 0);//drawing roi rectangle over image 
     //Mat Roi = src(Rec);//applying crop mask
     Mat Roi(src,Rec);//applying crop mask same as above
     delay(1);
     //Mat croppedImage;
     //Roi.copyTo(croppedImage);//copying Roi to new Mat
     size_t jpegSize = camera.createJpeg(IMAGE_HW, IMAGE_VW, Roi.data, Camera::FORMAT_GRAY);
     display_app.SendJpeg(camera.getJpegAdr(), jpegSize);
     delay(1);
}

  • I modified the argument of createJpeg. Not only that but it was necessary to change the size to 160x120.

    #include <Arduino.h>
    #include <Camera.h>
    #include <opencv.hpp>
    #include <DisplayApp.h>
    using namespace cv;

    #define IMAGE_HW 320
    #define IMAGE_VW 240

    Camera camera(IMAGE_HW, IMAGE_VW);
    static DisplayApp display_app;
    uint8_t gray_buf[1 * IMAGE_HW * IMAGE_VW]__attribute((section("NC_BSS"),aligned(32)));

    void setup() {
    camera.begin();
    }

    void loop() {
    Mat img_raw(IMAGE_VW, IMAGE_HW, CV_8UC2, camera.getImageAdr());//reading camera image
    Mat src;
    cvtColor(img_raw, src, COLOR_YUV2GRAY_YUYV);//converting to gray
    Rect Rec(0, 0, 160, 120);//defining roi
    rectangle(src, Rec, Scalar(255), 1, 8, 0);//drawing roi rectangle over image
    //Mat Roi = src(Rec);//applying crop mask
    Mat Roi(src, Rec);//applying crop mask same as above
    delay(1);
    Mat croppedImage;
    Roi.copyTo(croppedImage);//copying Roi to new Mat
    size_t jpegSize = camera.createJpeg(Rec.width, Rec.height, croppedImage.data, Camera::FORMAT_GRAY);
    display_app.SendJpeg(camera.getJpegAdr(), jpegSize);
    delay(1);
    }
  • In reply to Okamiya Yuuki:

    Thank you Okamiya Yuuki you saved my day.