Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How can I modify ROI and then add it to the original image using Python?

I am new to Python and OpenCV. My task is to perform some operations on the ROI of an image and then adding back that image to the original image. How could I achieve this? For example, I want to change the colour of the ROI image and then add it back. My code is given below:

for (i,c) in enumerate(contours_from_left_to_right):    
     cv2.drawContours(duplicate_img, [c], -1, (0,0,255), 3)
     cent_moment = cv2.moments(c)
     centroid_x = int(cent_moment['m10'] / cent_moment['m00'])
     centroid_y = int(cent_moment['m01'] / cent_moment['m00'])
     cv2.putText(duplicate_img, str(i+1), (centroid_x, centroid_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
     cv2.imshow('Contours from Left to Right', duplicate_img)
     cv2.waitKey(0)
 (x, y, w, h) = cv2.boundingRect(c)    
 print("Top-Left Corner=",(x,y), "width= ",w,"height =",h)      
 ROI = roi_img[y:y+h, x:x+w]    
 cv2.imwrite("ROI_{}.png".format(image_number), ROI)
 image_number += 1