Here we will cover the basics of OpenCV's storage of images, and how they can be manipulated.
For our first step into the world of computer vision, we must understand how an image is stored onto a program so that a computer can manipulate it. In OpenCV, an image is stored as a two-dimensional matrix with one outer matrix, and the inner matrices being color channels.
Pixel values stored as a matrix
How the image is represented by its pixel values
The actual image being displayed
But in order to even start playing around with these values, we need to understand how you can convert a given image into an image matrix. To do this, we will cover the foundation for OpenCV functions, cv2.imread().
cv2.imread() takes an image path from your working directory and stores it as a matrix.
The format for an image path should be "DIRECTORY\IMAGE.JPG" Where "DIRECTORY" is the folder where your photos are stored in the working directory, and "IMAGE.JPG" is the actual image you want to feed in.
After that, store the value of cv2. imread() into a variable, after placing the image path as an argument.
then use the cv2.imshow() function which takes in your matrix variable and the window name. The window name can be anything you want. This will display your image to make sure it is not corrupted and can be used.
then use the cv2.waitkey() function and insert the number 0 into its argument. This will allow the window to appear, and be closed when a key is pressed. any number other than 0 will represent a number in milliseconds until that window closes automatically. For example if I type 1000 into the argument, it will close the window automatically after one second.
When successfully completed, go ahead and run the code. You should see a new window pop up with your image in it.
import cv2 as cv
import numpy as np
## between the part where it says "Photos\" and ".jpg" enter the file name.
img=cv.imread('Photos\(_________).jpg')
## Fill in the name of the window.
cv.imshow('(_______)',img)
cv.waitKey(0)
## Fill in the numbers to change the color value of the pixel value at a given coordinate
img[()][()]=[(),(),()]
cv.imshow('(_______)',img)
cv.waitKey(0)
Legend:
img=your specified image matrix
x= an integer value
'windowname'=name of the given window
Functions:
cv2.imread('DIRECTORY\IMAGE.JPG')
cv2.imshow('windowname',img)
cv2.waitkey(x)