Extracting the pixel value of the image

Hello everyone

I am new to using OpenMV but I am having a few difficulties. I hope you guys can help give me some advice. I am struggling to transfer my python code in analysing the image to the device. At the moment I am stuck with extracting the pixel value in the image. I tried different approaches but keep getting error messages. Here is part of the code in python that I want to implement on the device:


img = Image.open(‘Image1.jpg’)

img1 = img.crop((175, 0, 245, 180))

pix=img1.load()

image_size = img1.size
analysed_array = zeros([image_size[1],1])

for i in range(0, image_size[1]):
for j in range(0, image_size[0]):
analysed_array[i,0] = analysed_array[i,0] + pix[j,i]


In this example code I am reading in the image taken from the camera on my computer. I want to convert the i by j array of the cropped image to a i by 1 array by summing along the x-axis (horizontal). This section of the code works well on python and I get the result I want but when I transfer this to OpenMV device I get errors for using “zeros” and “image.load()”. I am stuck and not sure how to proceed. What I want to achieve is finding the pixel value of each i and j position.

Hope you guys can help and guide on how to achieve my goal.

Thank you so much

Shasi

You’re trying to use bumpy like indexing on the image array. We don’t support that. Please read the API on the image module under the docs.

Mmm, under closer inspection it looks like you are just trying to use desktop python code on the camera, you cannot do that.

Anyway, you shouldn’t try to do any image manipulation in python on the pixel level. This will be far too slow. Please use the methods in the api.

Hi Nyameke

Thanks for your reply. I am sad to hear I cannot do a pixel level analysis. The main thing is I want to identify how the grayscale value of a dark line in a section of the image changes over time. I was hoping I could use the camera to take the image and measure this value. It would be much quicker than trying to upload the image elsewhere or transfer to a desktop to carryout this analysis.

Is there no other way of finding the grayscale or rgb values of the image array? I dont need to do any manipulation but knowing the value at least would be useful.

thank you
Shasi

get_pixel()

Will give you the price results. It’s just slow in python. If you want to do pixel level stuff use C code.

Or, use our library methods which likely do what you need.

Thanks Nyameke. This would be really helpful.

One last question, can I create an array and find the minimum value in the array? I know numpy functions such as zeros is not possible.

cheers
Shasi

Just iterate through the image and keep track of the minimum pixel value you see. If you keep the region of interest small the code will run decently fast.

Hi Nyamekye

I am happy with the slow processing time for my application. Its only a small section of the image between 5 to 10 pixels. But I am struggling to get the last part of the code working. Here is an example of what I like to achieve is below:

for i in range(0, 5):
pixel_value[i,0] = img1.get_pixel(0, i)

chosen_value = pixel_value[1,0]

But I cant carry out the above code as I get an error. I am sorry I am being quite annoying here but if I can at least get same pixel value in an array and read it out later, I do the rest using the example code I found on the IDE.

And the error message is and the line it’s from?

Hi Nyamekye

Apologises. I should have included the error in the last message. The first error I get is for this line of code:

pixel_value[i,0] = img1.get_pixel(0, i)

Nameerror: name pixel_value is not defined

If I use the following code, I don’t get an error.

pixel_value = img1.get_pixel(0, i)

so it seems to be an issue when I try to use index for the array. How could I store some of the pixel values in an array? Also how could I read the values out of the array? Like:

recovered_value = pixel_value[1,0]

cheers
Shasi

Hi,

I think Nyamekye already answered this you cant index like that, python doesn’t know what structure your trying to use. If you want to use a list you need to define it as

 mylist = []

for example before you use it. Then you can Insert a value into a certain index position of the list using the following notation:

mylist[index] = value

If you want to get a value from the array just use:

value  = mylist[index]

If you are trying to store the pixels values in an array use the following:

pixel_values = []
for i in range(0,5):
	pixel_values.append(img1.get_pixel(0, i))

This example could be made much more elaborate but thats the most basic way of showing you.

Hi GL-ITC and Nyamekye

Thank you so much for all your help. I have been struggling with the array. I couldn’t figure out how to add to the array. I greatly appreciate all your help and for your patience with me. Sorry for being so annoying.

cheers
Shasi