Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get co-ordinates and save image of the objects #7276

Open
shubham-shahh opened this issue Jan 22, 2021 · 7 comments · May be fixed by #7282
Open

How to get co-ordinates and save image of the objects #7276

shubham-shahh opened this issue Jan 22, 2021 · 7 comments · May be fixed by #7282

Comments

@shubham-shahh
Copy link

Hello,
i am using the following command
./darknet detector demo ./customcfg/tinyv3.data ./customcfg/yolov3-tiny.cfg ./weights/yolov3-tiny.weights ./videostest/test1.mp4 -i 0 -thresh 0.25
My question is, How can i save the image of the cropped object in a folder?

@stephanecharette
Copy link
Collaborator

From the CLI there is no magical way to automatically do that. You'd have to write some code to take the bounding boxes, extract the roi, and save them as new images.

@shubham-shahh
Copy link
Author

shubham-shahh commented Jan 22, 2021

sir, what do i have to edit in image.c?

From the CLI there is no magical way to automatically do that. You'd have to write some code to take the bounding boxes, extract the roi, and save them as new images.

@stephanecharette
Copy link
Collaborator

What? No, don't edit image.c! You need to learn to use the API. Or save the coordinates in a file (search for -ext_output in the readme) and use whatever language you are familiar with.

@shubham-shahh
Copy link
Author

shubham-shahh commented Jan 23, 2021

Hello Sir,
i found this in image_opencv.cpp
@AlexeyAB I think this is old piece of code which is not updated

// you should create directory: result_img static int copied_frame_id = -1; static IplImage* copy_img = NULL; if (copied_frame_id != frame_id) { copied_frame_id = frame_id; if(copy_img == NULL) copy_img = cvCreateImage(cvSize(show_img->width, show_img->height), show_img->depth, show_img->nChannels); cvCopy(show_img, copy_img, 0); } static int img_id = 0; img_id++; char image_name[1024]; sprintf(image_name, "result_img/img_%d_%d_%d_%s.jpg", frame_id, img_id, class_id, names[class_id]); CvRect rect = cvRect(pt1.x, pt1.y, pt2.x - pt1.x, pt2.y - pt1.y); cvSetImageROI(copy_img, rect); cvSaveImage(image_name, copy_img, 0); cvResetImageROI(copy_img);

if i compile after un commenting these lines

i get following error

/src/image_opencv.cpp:986:84: error: ‘class cv::Mat’ has no member named ‘width’
if(copy_img == NULL) copy_img = cvCreateImage(cvSize(show_img->width, show_img->height), show_img->depth, show_img->nChannels);
^~~~~
compilation terminated due to -Wfatal-errors.
Makefile:182: recipe for target 'obj/image_opencv.o' failed
make: *** [obj/image_opencv.o] Error 1

@shubham-shahh
Copy link
Author

shubham-shahh commented Jan 23, 2021

@AlexeyAB I Finally found the solution. I think after the update , "show_img" has been changed from type "Iplimage" to "cv::Mat" and the commented code wasn't updated based on the new conversion so to make the coomented lines work, we need to convert show_img to iplimage type by adding this line

IplImage* imggf = new IplImage(*show_img);

and change the line if(copy_img == NULL) copy_img = cvCreateImage(cvSize(show_img->width, show_img->height), show_img->depth, show_img->nChannels);

To
if(copy_img == NULL) copy_img = cvCreateImage(cvSize(imggf->width,imggf->height), imggf->depth, imggf->nChannels);

and cvCopy(show_img, copy_img, 0);

to
cvCopy(imggf, copy_img, 0);

I'll put up a pull request as well including some other topic related changes

@shubham-shahh
Copy link
Author

#7282

Here is my pull request where I've addressed the above problem.

@rg321
Copy link

rg321 commented Nov 2, 2021

Hi, can you try following code
in file src/image_opencv.cpp, show_img is Mat pointer.
Just crop desired portion out of Mat.

// you should create directory: result_img static int copied_frame_id = -1; // static IplImage* copy_img = NULL; if (copied_frame_id != frame_id) { copied_frame_id = frame_id; // if(copy_img == NULL) copy_img = cvCreateImage(cvSize(show_img->width, show_img->height), show_img->depth, show_img->nChannels); // cvCopy(show_img, copy_img, 0); } static int img_id = 0; img_id++; char image_name[1024]; sprintf(image_name, "result_img/img_%d_%d_%d_%s.jpg", frame_id, img_id, class_id, names[class_id]); CvRect rect = cvRect(pt1.x, pt1.y, pt2.x - pt1.x, pt2.y - pt1.y); cv::Mat copy_image = (*show_img)(rect); cv::imwrite(image_name, copy_image); // cvSetImageROI(copy_img, rect); // cvSaveImage(image_name, copy_img, 0); // cvResetImageROI(copy_img);

pull request created for the same
#8197

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants