Ask Your Question
0

i want to draw manually a rectagle on a region of interst in an image. in matlab i used by "imrect" command but i dont know what is it in python-opencv?

asked 2019-12-05 03:07:55 -0500

i want to draw manually a rectagle on a region of interst in an image. in matlab i used by "imrect" command but i dont know what is it in python-opencv?

edit retag flag offensive close merge delete

Comments

berak gravatar imageberak ( 2019-12-05 03:17:35 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-12-05 05:15:51 -0500

supra56 gravatar image

updated 2019-12-05 05:20:02 -0500

There are 2 ways to do this. Draw manually:

#!/usr/bin/python3.7
#Raspberry pi 3/4, OpenCV 4.1.2
#Date: 5th December, 2019

import cv2

img = cv2.imread('x.jpg')
c, r, w, h =  100, 100, 640,480
roi = img[r:r + h, c:c + w ]
cv2.imshow('ROI', roi)
cv2.waitKey(0)

And API:

#!/usr/bin/python3.7
#Raspberry pi 3/4, OpenCV 4.1.2
#Date: 5th December, 2019

import cv2 as cv

im = cv.imread("lena.jpg")

showCrosshair = False
fromCenter = False 
# Select ROI
r = cv.selectROI(im, fromCenter, showCrosshair)

# Crop image
imCrop = im[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]

# Display cropped image
cv.imshow("Image", imCrop)
cv.waitKey(0)
cv.imwrite('lena_roi.jpg', imCrop)
  1. Select a ROI and then press SPACE or ENTER button!
  2. Cancel the selection process by
    pressing c button!
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-12-05 03:07:55 -0500

Seen: 80 times

Last updated: Dec 05 '19