Reputation: 6107
is there a way to set a rectangular block of pixels to a colour in opencv (specifically in python) other than looping through them? e.g. some kind of 2d slice syntax or once an ROI is set change all inside.
i have tried im[0:100, 200:300] = (255,255,255)
with no luck
Upvotes: 0
Views: 3967
Reputation: 52646
im[0:100,200:300] = [255,255,255]
works fine for me.
Eg:
>>> im = cv2.imread('baboon.jpg')
>>> im[0:2,200:202]
array([[[180, 200, 181],
[164, 190, 166]],
[[170, 182, 164],
[124, 134, 118]]], dtype=uint8)
>>> im[0:100,200:300] = [255,255,255]
>>> im[0:2,200:202]
array([[[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255]]], dtype=uint8)
Another method is cv2.rectangle
cv2.rectangle(im,(200,0),(300,100),(255,255,255),-1)
But it seems cv2.rectangle is more faster (around 5x) than previous slicing method(as per my tesing)
Upvotes: 2
Reputation: 12827
You should have a look at CV.Rectangle, which does exactly what you want :) http://opencv.willowgarage.com/documentation/python/core_drawing_functions.html?highlight=rectangle#Rectangle
Upvotes: 2