You are on page 1of 1

import cv2 as cv

import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('lena.jpg',0)
imgBlurred = cv.GaussianBlur(img, (5,5), 0)
imgThresh = cv.adaptiveThreshold(imgBlurred, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C,
cv.THRESH_BINARY_INV, 11,2)
imgThreshCopy = imgThresh.copy()
npaContours, npaHierarchy = cv.findContours(imgThreshCopy, cv.RETR_EXTERNAL,
cv.CHAIN_APPROX_SIMPLE)
for npaContour in npaContours:
[intX, intY, intWidth, intHeight] = cv.boundingRect(npaContour)
cv.rectangle(img,
(intX, intY),
(intX + intWidth, intY + intHeight),
(0, 255, 0),
2)

img = cv.cvtColor(img, cv.COLOR_BGR2RGB)

titles = ['Original Image']


images = [img]
for i in range(1):
plt.subplot(1,1,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()

You might also like