To write a python program using OpenCV to do the following image manipulations. i) Extract ROI from an image. ii) Perform handwritting detection in an image. iii) Perform object detection with label in an image.
Anaconda - Python 3.7
Import necessary packages
Read the image and convert the image into RGB
Display the image
Set the pixels to display the ROI
Perform bit wise conjunction of the two arrays using bitwise_and
Display the segmented ROI from an image.
Import necessary packages
Define a function to read the image,Convert the image to grayscale,Apply Gaussian blur to reduce noise and improve edge detection,Use Canny edge detector to find edges in the image,Find contours in the edged image,Filter contours based on area to keep only potential text regions,Draw bounding boxes around potential text regions.
Display the results.
Import necessary packages
Set and add the config_file,weights to ur folder.
Use a pretrained Dnn model (MobileNet-SSD v3)
Create a classLabel and print the same
Display the image using imshow()
Set the model and Threshold to 0.5
Flatten the index,confidence.
Display the result.
import cv2
import numpy as np
image = cv2.imread('rose.jpg')
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.imshow('Original Image', image_rgb)
cv2.waitKey(0)
cv2.destroyAllWindows()
roi_mask = np.zeros_like(image_rgb)
roi_mask[100:300, 200:400, :] = 255
segmented_roi = cv2.bitwise_and(image_rgb, roi_mask)
cv2.imshow('Segmented ROI', segmented_roi)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
import matplotlib.pyplot as plt
def detect_handwriting(image_path):
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edges = cv2.Canny(blurred, 50, 150)
contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
min_area = 100
text_contours = [cnt for cnt in contours if cv2.contourArea(cnt) > min_area]
img_copy = img.copy()
for contour in text_contours:
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(img_copy, (x, y), (x + w, y + h), (0, 255, 0), 2)
img_rgb = cv2.cvtColor(img_copy, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.title('Handwriting Detection')
plt.axis('off')
plt.show()
image_path = 'img proj.jpg'
detect_handwriting(image_path)
config_file='ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
frozen_model='frozen_inference_graph.pb'
model=cv2.dnn_DetectionModel(frozen_model,config_file)
classLabels = []
file_name='Labels.txt'
with open(file_name,'rt')as fpt:
classLabels=fpt.read().rstrip('\n').split('\n')
print(classLabels)
print(len(classLabels))
img=cv2.imread('cow.jpg')
plt.imshow(img)
plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
model.setInputSize(320,320)
model.setInputScale(1.0/127.5)#255/2=127.5
model.setInputMean((127.5,127.5,127.5))
model.setInputSwapRB(True)
ClassIndex,confidence,bbox=model.detect(img,confThreshold=0.5)
print(ClassIndex)
font_scale=3
font=cv2.FONT_HERSHEY_PLAIN
for ClassInd,conf,boxes in zip(ClassIndex.flatten(),confidence.flatten(),bbox):
cv2.rectangle(img,boxes,(0,0,255),2)
cv2.putText(img,classLabels[ClassInd-1],(boxes[0]+10,boxes[1]+40),font,fontScale=font_scale,color=(255,0,0),thickness=1)
plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
![image](https://private-user-images.githubusercontent.com/120206067/327918641-49f0d3f9-498d-46e6-aa17-309f973bdd9e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU0MDI4NDksIm5iZiI6MTcyNTQwMjU0OSwicGF0aCI6Ii8xMjAyMDYwNjcvMzI3OTE4NjQxLTQ5ZjBkM2Y5LTQ5OGQtNDZlNi1hYTE3LTMwOWY5NzNiZGQ5ZS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwOTAzJTJGdXMtZWFzdC0xJ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ub2jqSJ7hefnAcWr79tPV_LBkf1FFJ3PcvF1BzuyKMk)
![image](https://private-user-images.githubusercontent.com/120206067/327918736-f1e8d338-3ff2-4d58-b4d8-2e1feb0a5a5e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU0MDI4NDksIm5iZiI6MTcyNTQwMjU0OSwicGF0aCI6Ii8xMjAyMDYwNjcvMzI3OTE4NzM2LWYxZThkMzM4LTNmZjItNGQ1OC1iNGQ4LTJlMWZlYjBhNWE1ZS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwOTAzJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDkwM1QyMjI5MDlaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1jZTUxMTBjYzc2NTk1NjUzNDNkOWI2NmY0MjJhN2Q0OTAzNjMxOTA4MDY3ZjQ0MDRlMmYwNGM3ZmE1MDU3YzhhJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.GrTNiqagf5CwuOlkgFD7cUHZ8AojUwMqPh1lZ_uKhAQ)
![dog](https://private-user-images.githubusercontent.com/118423842/327125906-2deb9655-7a0c-447d-8ad3-e9eca3e525ed.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU0MDI4NDksIm5iZiI6MTcyNTQwMjU0OSwicGF0aCI6Ii8xMTg0MjM4NDIvMzI3MTI1OTA2LTJkZWI5NjU1LTdhMGMtNDQ3ZC04YWQzLWU5ZWNhM2U1MjVlZC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwOTAzJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDkwM1QyMjI5MDlaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1hOWU3NGIyZWRkMWViNGI0ZDczZTUzM2E3YmJkY2MzNzVmOGVmYWFjMjE1YmUxM2Q5ZTc4YzM1MWI0MjI5YmFjJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.ljbqFplBSZ2yaDgMBYk_M1T1Xw2pi2K55NMORCVEIIk)
Thus, a python program using OpenCV for following image manipulations is done successfully