Starting to get interested in image processing and its applications? This project is ideal as a first step to understand the fundamentals of any image- pixels. Pixels are small boxes of color that constitute any image. We throw around the word ‘pixels’ a lot in our everyday life, like when describing your mobile phone camera to a friend. But, understanding what they actually are is important in image processing.
The aim of this project is to build a program that will allow us to view the color components (Red, Green, Blue) in each pixel. We capture the image using the Raspberry Pi camera, and then pick the desired area. This project is the first step in larger projects like image matching, forgery detection and deep learning.
Hardware Requirements:
- Raspberry Pi (I used, model 3 B +)
- Camera module
- Power cable
- Monitor
- HDMI connector
- USB or Bluetooth mouse
- USB or Bluetooth keyboard
Setup:
- Connect the Raspberry Pi camera module (for any help on that, check out our article on that https://iot4beginners.com/how-to-capture-image-and-video-in-raspberry-pi/ ).
- Use the HDMI cable to connect your Raspberry Pi to a display
- Connect your mouse and keyboard as well (through USB or Bluetooth)
- Make sure to install NumPy and OpenCV library on your Raspberry Pi before you start
What is OpenCV?
OpenCV is an open-source library for computer vision, machine learning, and image processing. It is especially useful in real-time systems which makes it a desirable tool to learn and implement. When it integrated with NumPy, Python can process OpenCV for analysis. This is why when coding with OpenCV in Python we also import NumPy, which is a library that is used with Python to perform matrix computations, and linear algebra among other complex mathematical functions
Code:
import numpy as np
import cv2
import picamera
import time
#take a realtime image
with picamera.PiCamera() as camera:
camera.resolution = (512,512)
camera.start_preview()
time.sleep(5)
camera.capture("/home/pi/Desktop/microscope/rtimg.jpg")
#make a copy of the original image
img = cv2.imread('rtimg.jpg', cv2.IMREAD_UNCHANGED)
cv2.imwrite('rtimg2.jpg', img)
while True:
#read image
im = cv2.imread('rtimg2.jpg',cv2.IMREAD_UNCHANGED)
#select ROI
r = cv2.selectROI(im)
#crop
imCrop = im[int(r[1]):int(r[1]+r[3]),int(r[0]):int(r[0]+r[2])]
#zoom
scale_percent = 400
#scale_percent = int(input ("enter : "))
width = int(imCrop.shape[1] *scale_percent / 100)
height = int(imCrop.shape[0] *scale_percent / 100)
dim =(width,height)
imZoom = cv2.resize(imCrop,dim,interpolation = cv2.INTER_AREA)
#save cropped
cv2.imwrite('rtimg2.jpg', imZoom)
#ask for continuation
def click_and_crop(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
refPt = [(x, y)]
cropping = True
elif event == cv2.EVENT_LBUTTONUP:
refPt.append((x, y))
cropping = False
cv2.rectangle(image, refPt[0], refPt[1], (0, 255, 0), 2)
cv2.imshow("image", image)
#show zoomed image
img = cv2.imread('rtimg2.jpg'.jpg')
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation for the code:
To write the code for this project, we begin by importing the libraries needed for running the code.
import numpy as np
import cv2
import picamera
import time
Now take a picture using the Raspberry Pi camera, the code below will give you 5 seconds time to position your camera while showing you a preview of the image on the screen. Also we make a temporary copy of the image in order to retain the original image.
#take a realtime image
with picamera.PiCamera() as camera:
camera.resolution = (512,512)
camera.start_preview()
time.sleep(5)
camera.capture("/home/pi/Desktop/rtimg.jpg") #insert name and desired location
#make a copy of the original image
img = cv2.imread('rtimg.jpg', cv2.IMREAD_UNCHANGED)
cv2.imwrite('rtimg2.jpg', img)
We now write a code to allow the user to use a rectangular ROI to select the part of image they want to zoom into.
while True:
#read image
im = cv2.imread('rtimg2.jpg',cv2.IMREAD_UNCHANGED)
#select ROI
r = cv2.selectROI(im)
#crop
imCrop = im[int(r[1]):int(r[1]+r[3]),int(r[0]):int(r[0]+r[2])]
#zoom parameters
scale_percent = 400 #feel free to vary this parameter
width = int(imCrop.shape[1] *scale_percent / 100)
height = int(imCrop.shape[0] *scale_percent / 100)
dim =(width,height)
imZoom = cv2.resize(imCrop,dim,interpolation = cv2.INTER_AREA)
#save cropped
cv2.imwrite('rtimg2.jpg', imZoom)
The program must reiterate the selecting process till the user has selected only the area desired to be zoomed into, thus we define a mouse click event to select again.
#ask for continuation
def click_and_crop(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
refPt = [(x, y)]
cropping = True
elif event == cv2.EVENT_LBUTTONUP:
refPt.append((x, y))
cropping = False
cv2.rectangle(image, refPt[0], refPt[1], (0, 255, 0), 2)
cv2.imshow("image", image)
We now finish the code by displaying the image on which we can scroll using the mouse to view the component values of each pixel.
#show zoomed image
img = cv2.imread('rtimg2.jpg'.jpg')
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Outputs:
This project not only is useful as a baseline for larger deep learning projects, but is also useful as a practical approach to understanding pixels. Viewing things with our own eyes makes comprehending it much easier. And even though pixels in images are much smaller than what meets the eye, through this project it makes it easy to see what makes the big picture so beautiful.