0

I am reading the raw video data from the read buffer using

cv::Mat imgbuf(Size(640, 480), CV_8UC3, &mem[0], (640*3));

This variable imgbuf I am passing to face detection algorithm which detects the face & draws the rectangle around the face. after that I am getting output something like

output-without-resize-function

I tried with below code where I am performing resize operation before pass to face detection algorithm. by using this method it is working fine. but without resizing function I am getting noticeable output with rectangle around the face.

output-with-resize-function

 while(1)
 {
 unsigned char *mem = (unsigned char*)mmap(NULL, page_offset + len,
 PROT_READ |PROT_WRITE, MAP_PRIVATE, fd, page_base); 
 cv::Mat imgbuf(Size(640, 480), CV_8UC3, &mem[0], (640*3));

 cv::resize(imgbuf,imgbuf,(640,480)); //Dummy function to get the right output.


 auto result = v->facedetection(imgbuf);
    for (const auto &r : result.rects) {
 cv::rectangle(imgbuf,cv::Rect{ cv::Point(r.x * imgbuf.cols, r.y * 
 imgbuf.rows),cv::Size{(int)(r.width * imgbuf.cols), (int)(r.height * 
   imgbuf.rows) } },0xff);

   }
  imshow("face-detection", imgbuf);
  waitKey(1);

can anybody help be to sort out this problem

nathancy
  • 42,661
  • 14
  • 115
  • 137
Gagandeep
  • 33
  • 1
  • 2
  • 6
  • Do an "imshow" right after cv::Mat imgbuf(Size(640, 480), CV_8UC3, &mem[0], (640*3)); and check if you have a valid image or not (before resizing). – MeiH Apr 20 '19 at 10:13
  • @Meisam I checked before resizing function, I have the valid image. & if I remove rectangle function then also I am getting right output, It seems like rectanle function directly writing to memory. – Gagandeep Apr 21 '19 at 12:52
  • Can anybody help me to sort out this metter – Gagandeep Apr 22 '19 at 04:47
  • your rectangle command is a little weird for me. What information exactly is in "result.rects"? can you convert it to a cv::Rect, then try rectangling? – MeiH Apr 22 '19 at 05:54
  • @Meisam yes I tried with below code for the same but results are same. auto R= cv::Rect{ cv::Point(r.x * imgbuf.cols, r.y * imgbuf.rows),cv::Size{(int)(r.width * imgbuf.cols), (int)(r.height * imgbuf.rows) } }; cv::rectangle(imgbuf, R , 0xff); – Gagandeep Apr 22 '19 at 11:54
  • use a "cout" and print the Rect (R) values. – MeiH Apr 22 '19 at 12:09
  • values like this -> [107 x 198 from (328, 77)] – Gagandeep Apr 22 '19 at 12:37

1 Answers1

0

Test this method:

unsigned char *mem = (unsigned char*)mmap(NULL, page_offset + len,
 PROT_READ |PROT_WRITE, MAP_PRIVATE, fd, page_base); 

cv::Mat imgbuf(480,640, CV_8UC3, &mem[0]);
cv::Mat img_2, img_3;
cv::resize(imgbuf,img_2,cv::Size(640,480));
img_2.copyTo(img_3);
auto result = v->facedetection(img_2);

for (const auto &r : result.rects)
{
   cv::Rect myR = cv::Rect(r.x * img_2.cols, r.y * img_2.rows, (int)(r.width * img_2.cols), 
   (int)(r.height * img_2.rows));

   cv::rectangle(img_3,myR,Scalar(0, 0, 255), 1);
}
imshow("Result", img_3);
waitKey(0);

After getting a valid result you can optimize this and use less of "Mat"s.

MeiH
  • 1,763
  • 11
  • 17
  • I know I will work, but I not clear why we can't imgbuf variable, why do we need to use img_2.copyTO(img_3) function? what are the differences between img_2 & imgbuf variable? can you help me to answer this question? – Gagandeep Apr 23 '19 at 07:53
  • @Gagandeep It's probably the "facedetection", I don't know how exactly this function works, but it looks like somehow this function modifies and changes the input matrix. – MeiH Apr 23 '19 at 08:11
  • why it is not affecting the img_2 variable? – Gagandeep Apr 23 '19 at 09:14
  • @Gagandeep It affects that too, that's why I use img_3 for rectangling. – MeiH Apr 23 '19 at 10:15