Reputation: 169
What I want to do is to increase the size of these boxes inplace, like if the box has a size of 100x200 i want to be 120x240 ie a 20 percent increase in size.
Resizing an croping the image will not work for all images as I am using it as a mask for another image(s) and if the position is changed the next step will not work.
I have been searching for a way to do it but was unable to find it.
I am using python 3.9.4
Upvotes: 2
Views: 218
Reputation: 548
You can use connected components function from OpenCV to detect the white boxes. Now once you the center, height, width of all the boxes you can simply increase the size of them and replace them on a black image.
Official documentation of the function : Structural Analysis and Shape Descriptors
import cv2
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255,
cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
output = cv2.connectedComponentsWithStats(thresh, args["connectivity"],cv2.CV_32S)
(numLabels, labels, stats, centroids) = output
The above code loaded the image and finds out all the components in the image
now you can iterate over them to find the required component.
# loop over the number of unique connected component labels
for i in range(0, numLabels):
# if this is the first component then we examine the
# *background* (typically we would just ignore this
# component in our loop)
if i == 0:
text = "examining component {}/{} (background)".format(
i + 1, numLabels)
# otherwise, we are examining an actual connected component
else:
text = "examining component {}/{}".format( i + 1, numLabels)
# print a status message update for the current connected
# component
print("[INFO] {}".format(text))
# extract the connected component statistics and centroid for
# the current label
x = stats[i, cv2.CC_STAT_LEFT]
y = stats[i, cv2.CC_STAT_TOP]
w = stats[i, cv2.CC_STAT_WIDTH]
h = stats[i, cv2.CC_STAT_HEIGHT]
area = stats[i, cv2.CC_STAT_AREA]
(cX, cY) = centroids[i]
Upvotes: 1