Ask Your Question
1

Python faster pixel access

asked 2017-12-20 15:55:30 -0600

sjhalayka gravatar image

I'm using a double for loop to iterate over an image, and it's very slow:

for j in range(0, hsv_frame.shape[0]):
    for i in range(0, hsv_frame.shape[1]):
        h = frame[j, i, 0]
        s = frame[j, i, 1]
        v = frame[j, i, 2]

Is there any other way to iterate over the image?

edit retag flag offensive close merge delete

Comments

try to avoid per-pixel operations in general. both numpy and opencv are very fast, when you use vectorized functions. the problem is usually more: finding out, if there's something builtin for your case already, and how it is called.

berak gravatar imageberak ( 2017-12-21 02:11:57 -0600 )edit

I understand parallelization. Thanks for the insight!

sjhalayka gravatar imagesjhalayka ( 2017-12-21 09:21:43 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2017-12-21 00:54:25 -0600

moHe gravatar image
h, s, v = cv2.split(hsv_frame)
edit flag offensive delete link more

Comments

Absoultely!

supra56 gravatar imagesupra56 ( 2017-12-21 06:04:37 -0600 )edit

you don't even need the split(), numpy has proper "slices". e.g. it could be:

h = [:,:,0]
s = [:,:,1]
v = [:,:,2]
berak gravatar imageberak ( 2017-12-21 07:53:24 -0600 )edit
1

Thanks for your help guys! I'm just learning Python. I'll try both methods to see what ends up fastest.

sjhalayka gravatar imagesjhalayka ( 2017-12-21 08:46:52 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-12-20 15:55:30 -0600

Seen: 3,193 times

Last updated: Dec 21 '17