Python: get average colour outside cropped area

Question:

I have an image that has an object which I cropped out of the image using Canny filter

import cv2
import numpy as np
from matplotlib import pyplot as plt
from PIL import Image

# load image
img = cv2.imread('dataset/example.png')

#.
#.
#.
#.
#.

# canny edge detection then find the non-zero min-max coords of canny 

#.
#.
#.
#.
#.

# ROI
roi = img[y1:y2, x1:x2]
## crop ROI
cropped = np.array(img)
cropped[y1:y2, x1:x2] = (0, 0, 0)
bg = Image.fromarray(cropped)

This is the result I get:

![enter image description here

Is there a way to select the region outside the crop area (black box)? Basically selecting the inverse of cropped[y1:y2, x1:x2] and then getting the average colour of that background?

Asked By: Anna1994

||

Answers:

You can use cv2.mean with a mask:

# create a mask from coordinages
mask = cv2.rectangle(np.zeros(img.shape[:2],'uint8'), (y1,x1), (y2,x2), 255, -1)

# out
means = cv2.mean(img, mask=mask)
Answered By: Quang Hoang

You cannot crop non-4 vertex polygons – remember you are working with matrices. If you want to get the contours of the non-black region, you can first get a binary mask using a threshold value of 0. This will render everything above that value in white. Then get the contours of that binary mask, like this:

# importing cv2 & numpy
import numpy as np
import cv2

# image path
path = "C://opencvImages//"
fileName = "squareTest.png"

# Reading an image in default mode:
inputImage = cv2.imread(path + fileName)

# Grayscale conversion:
grayscaleImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)

# Fixed Thresholding:
thresholdValue = 0
_, binaryImage = cv2.threshold(grayscaleImage, thresholdValue, 255, cv2.THRESH_BINARY)

This is the mask you obtain:

Now, simple get the contours:

# Find the contours on the mask image:
contours, hierarchy = cv2.findContours(binaryImage, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

# Draw the contours on the mask image:
cv2.drawContours(inputCopy, contours, -1, (255, 0, 0), 3)

This is the result:

Now, if you want the mean BGR(A) value of the non-black region, use the binary mask we got and pass it to cv2.mean as mask, like this:

means = cv2.mean(inputImage, mask=binaryImage)

You get:

(130.01283431634118, 223.66963836747732, 121.75817119126356, 0.0)
Answered By: stateMachine