V
V
Vincent2019-06-09 11:17:13
Python
Vincent, 2019-06-09 11:17:13

How to copy ROI from image when working with OpenCV?

Hello, here is my code:

img = cv2.imread('data\\Hunt.jpg', cv2.CAP_MODE_GRAY)
cv2.imshow('w', img)
ROI = []
Roi = 0

while (Roi != (0,0,0,0)):
    Roi = cv2.selectROI('w',img)
    os.system('cls')
    if Roi != (0,0,0,0):
        ROI.append(img[Roi[0]:Roi[1], Roi[2]:Roi[3]])
    pass
cv2.destroyAllWindows()

It can be seen that when ROI is selected, four values ​​are written to the Roi variable, the first two, apparently, are the coordinates of the upper left corner of the region, the last two, respectively, the lower right corner. Since I only need ROI from the original image, I imagine it will act as their holder, so to speak. However, I can't get all the pixels of this black and white image by coordinates. On the off-site OpenCV there is an example using And then everything is written normally in the assigned variable, but there is a color image in the example, perhaps because of this, in my case this does not work, because it only assigns an empty array Since I am interested in several areas of ROI , then a one-time pixel cutting function, if one exists, will not work here.
img[280:340, 330: 390]
5cfcbf0f204c2451065381.png
PS: I tried to use the cv2.copyMakeBorder() function, which gives the original image and coordinates, only there they are designated as top, bottom, left, right, but with all the flags of this function, it only copied the entire original image, which now looked different

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question