Jump to content

How to change contrast and brightness of an image with Python (Specifically OpenMV)?

Mr.Meerkat
Go to solution Solved by Mr.Meerkat,
8 hours ago, Azgoth 2 said:

 

How does OpenMV deal with images?  Does it use something like a 2d array of pixel values?  If so you could use some of the tools from probably the Numpy or Scipy libraries to clip your pixel values at a certain level (you'd probably need to play with the level a bit to find a good one).  Or, if you could use the PIL library:


from PIL import Image, ImageEnhance
im = Image.Image("/path/to/saved/image")
contrast = ImageEnhance.Contrast(im)
contrast = contrast.enhance(FACTOR) # set FACTOR > 1 to enhance contrast, < 1 to decrease

# either save the image...
contrast.save("/path/to/new/location")

# or cast to a numpy array
import numpy as np
im_array = np.array(contrast.getdata())

 

you can't import libraries in OpenMV

 

Read a different section of the OpenMV docs and realised that I could just control the actual sensor instead :P (rather than editing the image afterwards)

However, found out it doesn't work as it still struggles detecting the correct thing and it did not increase the detection range :/)

 

Thanks anyway :) 

The problem:

The code for an robotic arm, specifically the part that senses (a camera) where the coffee is and dispenses hot water on top of it is currently having trouble detecting the correct object if there is any other round black object within its FOV or its unable to detect it at all if the 'coffee' is too far away.

 

Sample image of the picture taken by the camera:

Spoiler

596c1f1ce0ab6_Betterscreenshotofthecoffeething.PNG.4d03ac7ebae52951a346580bd30a9c97.PNG

 

So through photoshop, we can see that if we tweak contrast (and maybe brightness), we can remove/soften some of the surrounding features which helps the camera to detect the 'coffee' better.

 

Photo shopped images:

Spoiler

Contrast 100, stock brightness

596c2020dd20f_Contrast100brightness0.PNG.68ddf563ca175cd36dc57a3404c286cc.PNG

 

Contrast 100, brightness 75

596c2025e3cfc_Contrast100brightness75.PNG.832c034f74b97e0e947d605bd7af8ced.PNG

 

Currently the code is this (pastebin won't load in this country I'm on 'holiday' in [not Scotland] so...spoiler it is :/): 

Spoiler

import sensor, image, time

from pyb import UART

uart = UART(3,9600)

test = 'f'+'dfdfdfd'+str(12)

green_threshold   = (   0,   80,  -70,   -10,   -0,   30)

gray=(0,80)

coffee=(7,20, -2,15, 0,15)

region_min=30

region_max=200

black_ratio=0.5

num_white=3

white_threshold=115

deviation_length_width=20

sensor.reset()

sensor.set_pixformat(sensor.GRAYSCALE)

sensor.set_framesize(sensor.QQVGA)

sensor.skip_frames(10)

sensor.set_whitebal(False)

clock = time.clock()

data1=bytearray(6)

while(True):

	command_req=0

	out_xy=0

	if 0x55==uart.readchar() and 0x1A==uart.readchar() and 0x55==uart.readchar() and 0x1A==uart.readchar() and 0x01==uart.readchar()and 0x01==uart.readchar():

		command_req=1

	if command_req==1:

			out_buffer=chr(0x55)+chr(0x1A)+chr(0x55)+chr(0x1A)

			clock.tick()

			img = sensor.snapshot()

			blobs = img.find_blobs([gray])

			if blobs:

				 for b in blobs:

						 if b[2]>region_min and b[2]<region_max and b[3]>region_min and b[2]<region_max and b[4]>b[2]*b[3]*black_ratio:

							for i in range(1,num_white+1):

								if img.get_pixel((b[0]+i),(b[1]+i))<white_threshold :

									break

								else :

									 if i==num_white :

										 if abs(b[2]-b[3])<deviation_length_width:

											out_xy=1

											temp1=0

											temp2=0

											img.draw_rectangle(b[0:4])

											img.draw_cross(b[5], b[6])

											out_buffer+=chr(10)

											out_buffer+=chr(2)

											temp1=int(b[5]/128)

											temp2=b[5]-128*temp1

											out_buffer+=chr(temp1)

											out_buffer+=chr(temp2)

											temp1=int(b[6]/128)

											temp2=b[6]-128*temp1

											out_buffer+=chr(temp1)

											out_buffer+=chr(temp2)

											temp1=int(b[2]/128)

											temp2=b[2]-128*temp1

											out_buffer+=chr(temp1)

											out_buffer+=chr(temp2)

											temp1=int(b[3]/128)

											temp2=b[3]-128*temp1

											out_buffer+=chr(temp1)

											out_buffer+=chr(temp2)

											out_buffer+=chr(0)

											uart.write(out_buffer)

			if(out_xy==0) :

				out_buffer=out_buffer+chr(0x01)+chr(0x03)

				uart.write(out_buffer)

 

(Actual problem)

So I'm piss poor at Python (never 'properly' used it before but do have a really basic understanding of it) and online documentation isn't super helpful so here I am asking here on how you adjust the contrast and brightness of a picture in Python (specifically openMV) unless you know a easier and a less computational hungry method :D 

 

Thanks guys! 

 

On a side note, this is what it looks like when it is able to detect the 'coffee'

Spoiler

596c22b2b2edc_Coffeedetected.PNG.11546473fbc2d79b296cd589bc6f1bbe.PNG

 

Looking at my signature are we now? Well too bad there's nothing here...

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

What? As I said, there seriously is nothing here :) 

Link to comment
Share on other sites

Link to post
Share on other sites

 

How does OpenMV deal with images?  Does it use something like a 2d array of pixel values?  If so you could use some of the tools from probably the Numpy or Scipy libraries to clip your pixel values at a certain level (you'd probably need to play with the level a bit to find a good one).  Or, if you could use the PIL library:

from PIL import Image, ImageEnhance
im = Image.Image("/path/to/saved/image")
contrast = ImageEnhance.Contrast(im)
contrast = contrast.enhance(FACTOR) # set FACTOR > 1 to enhance contrast, < 1 to decrease

# either save the image...
contrast.save("/path/to/new/location")

# or cast to a numpy array
import numpy as np
im_array = np.array(contrast.getdata())

 

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Azgoth 2 said:

 

How does OpenMV deal with images?  Does it use something like a 2d array of pixel values?  If so you could use some of the tools from probably the Numpy or Scipy libraries to clip your pixel values at a certain level (you'd probably need to play with the level a bit to find a good one).  Or, if you could use the PIL library:


from PIL import Image, ImageEnhance
im = Image.Image("/path/to/saved/image")
contrast = ImageEnhance.Contrast(im)
contrast = contrast.enhance(FACTOR) # set FACTOR > 1 to enhance contrast, < 1 to decrease

# either save the image...
contrast.save("/path/to/new/location")

# or cast to a numpy array
import numpy as np
im_array = np.array(contrast.getdata())

 

you can't import libraries in OpenMV

 

Read a different section of the OpenMV docs and realised that I could just control the actual sensor instead :P (rather than editing the image afterwards)

However, found out it doesn't work as it still struggles detecting the correct thing and it did not increase the detection range :/)

 

Thanks anyway :) 

Looking at my signature are we now? Well too bad there's nothing here...

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

What? As I said, there seriously is nothing here :) 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×