Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes! You can see it. It is because you are using too many cv.waitkey(0). Meaning, you will see original image's window, then close it. You will see immediately Bounding Rectangle' s window. open Then close Bounding Rectangle' s window.. Then immediately Approx Poly DP's window open. If you want to see all windows on fly, you will have to rem it out 2 waitKey() and leave one at bottom. Code:

#!/usr/bin/env python3
#Raspberry pi 4b, Buster Ver10, OpenCV 4.1.0
#Date: 12th July, 2019

import numpy as np
import cv2

image = cv2.imread('E:\\DOWNLOADS\\Computer Vision\\misc\\house.jpg')
orig_image = image.copy()
cv2.imshow('Original Image', orig_image)
#cv2.waitKey(0)

# Grayscale and Binarize
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)

# Find Contours
contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

# Iterate through each contour
for c in contours:
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(orig_image, (x, y), (x+w, y+h), (255, 0, 255), 2)
    cv2.imshow('Bounding Rectangle', orig_image)
#cv2.waitKey(0)

# Iterate through each contour and compute the approx contour
for c in contours:
    accuracy = 0.03 * cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, accuracy, True)
    cv2.drawContours(image, [approx], 0, (255, 255, 0), 2)
    cv2.imshow('Approx Poly DP', image)

cv2.waitKey(0)
cv2.destroyAllWindows()