0

I have a problem here during cropping and saving image by using opencv.
I'm trying to crop by using cv2.SelectROI function but after I drag on the image, cv.2imshow won't work properly.
Here's my code:

import cv2, numpy as np
img = cv2.imread('C:/git/ML/Image/colorful.jpg')
x,y,w,h = cv2.selectROI('img', img, False) 
if w and h:
  roi = img[y:y+h, x:x+w]
  cv2.imshow('cropped', roi) 
  cv2.moveWindow('cropped', 0, 0) 
  cv2.imwrite('cropped2.jpg',roi) 

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

print(x,y,w,h)

I've tried to change directory in various ways, put imshow method just before selectROI but none of them worked so far.

cv2.imshow itself shouldn't be a problem because when I don't use selectROI and just manually code the cropping performance from start to finish(by defining mouseleftbutton click, drag, leftbuttonup one by one), cv2.imshow, cv2.movewindow and cv2.imwrite works just fine.

also, not confident that the code itself have interal problem because in other computer, those activities(dragging, cropping, open in new window, save) seems to be working just fine.

is there a possibility that i haven't installed sth that should be needed in order to run selectROI..?

Anyways.. any comments will be much appreciated. Plz help me.

sourab maity
  • 1,025
  • 2
  • 8
  • 16
gi3265
  • 1
  • 2

0 Answers0