Open CV (https://opencv.org) is a library available to Python (and other languages) to assist with image based machine learning. I am looking to turn an image into pixel values and then turn a file of values back into an image.
I am going to be using XKCD #1696 (AI Research) as the image test for my project. It is available from: https://xkcd.com/1696/
XKCD #1696
Open Computer Vision (OpenCV) uses BGR values for pixels. This is the value of Blue (B), Green (G) and Red (R) of the pixels, and the value is between 0 and 255. Why does OpenCV use BGR and not RGB? It seems to be because BGR was a standard when the OpenCV library was being created (see: https://www.learnopencv.com/why-does-opencv-use-bgr-color-format/ ).
I used pip install opencv-python to install cv2 (OpenCV).
Function to turn data into a CSV of dictionary values
Please note:The screen grabs show the correct formatting as formatting is sometimes lost in my blog posts.
cv2.imread(”) opens the image file. If the image file is not in the same location as the Python program then the full file path should be used and not just the filename.
img_height, img_width = img.shape[:2] reads the size of the image file, which is then used to loop through the image pixel by pixel and gather the pixel values in the line pixel_value = img[x,y,:]. X, Y are the co-ordinate values and : transforms into the BGR value.
Function to turn image into CSV of data
When run the functions create either a CSV containing lines of pixel values (X, Y co-ordinates and then BGR values) OR a CSV of pixel values from dictionary values (X, Y co-ordinates and then BGR values).
Breaking image down to pixel location (X,Y) and pixel value (B G R)
I originally wrote just the CSV function with each pixel being a line of data but after consideration added the dictionary CSV function as I started thinking about how to best extract the data from the CSV later on (a.k.a. part 2 of my project).
Related
Published by Geek_Dude
I'm a tech enthusiast that enjoys science, science fiction, comics and video games - pretty much anything geeky.
View all posts by Geek_Dude
One thought on “Turning Images Into Values (Python)”
One thought on “Turning Images Into Values (Python)”
Comments are closed.