calculate the pixel ratio of two halves of an image

JG1981

I'm trying to calculate the pixel ratio of two halves of an image split vertically and horizontally along its centroid. The goal is to see how symmetrical/asymmetrical the image is. Below is the code, the original image, and an image showing what I'm trying to do.

So far, I've thresholded the image, created a contour around its perimeter, filled that contour, and calculated and labeled the centroid.

I'm stuck on how to (a) split the contour into two, and (b) calculate the pixel ratios between the two halves of the contour image (just the black parts. Thanks for any advice and/or help.

# import packages
import argparse
import imutils
import cv2

# construct argument parser
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
    help="path to the input image")
args = vars(ap.parse_args())

# load the image
image = cv2.imread(args["image"])

# convert it to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# threshold the image
(T, threshInv) = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)

# find outer contour of thresholded image
cnts = cv2.findContours(threshInv.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

# loop over the contour/s for image moments
for c in cnts:
    #  compute the center of the contour
    M = cv2.moments(c)
    #  calculate the centroid
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])

# draw and fill the contour on the image
cv2.drawContours(image, [c], -1, (0, 0, 0), thickness=cv2.FILLED)

# draw the centroid on the filled contour
cv2.circle(image, (cX, cY), 7, (255, 0, 0), -1)

# show the image
cv2.imshow("Image", image)
cv2.waitKey(0)

Original image:

enter image description here

Goal:

enter image description here

Jeru Luke

Part 1:

Images can be cropped respectively as shown below

top_half = image[0:cY, :]
bottom_half = image[cY:, :]

left_half = image[:, 0:cX]
right_half = image[:, cX:]

Part 2:

To calculate the ratio, let us just take any one channel of the above 4 cropped images. The channel would be a binary image consisting of white (255) pixels and black (0) pixels only. We will count the number of black pixels in each half and divide:

top_half = top_half[:,:,1]
bottom_half = bottom_half[:,:,1]
left_half = left_half[:,:,1]
right_half = right_half[:,:,1]

All the above are single channel images

top_bottom_ratio = int(np.size(top_half) - np.count_nonzero(top_half) / np.size(bottom_half) - np.count_nonzero(bottom_half)

np.size() gives total number of pixels in the image

np.count_nonzero() gives number of white pixels

You can do the same to find ratio between left and right halves

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to calculate device pixel ratio

Calculate ratio two tables

Issue concating two halves of a video with an image

How to calculate Image compression ratio?

How to horizontally swap two halves of an image in python opencv

Calculate total number of pixel in an image

Dividing a RelativeLayout into two halves

Split a row into two halves?

Splitting a sentence into two halves

Two halves of Java JButton (+/-)

separate an array into two halves

Is there a way to calculate the pixel length by marking two points on an image using android studio?

Calculate image ratio and apply value to query string

Calculate the ratio of area of different colors in an image

How to calculate the ratio using two hashmaps in java

How to aggregate two numerical variables then calculate the ratio

Calculate a ratio from two MySQL SELECT statements

Recalculating pixel coordinates after making an image smaller but the same aspect ratio

Retina - Correlation between device pixel ratio and size of image?

Calculate pixel distance from centre of image

Calculate color of an Pixel for a transparent application background image

Calculate possible image dimensions from pixel count

split std::bitset in two halves?

Calculate ratio every two rows with partial string matches

How to programmatically calculate the contrast ratio between two colors?

How to calculate a ratio of two dataframes with unevenly spaced values in R?

calculate ratio of two factors for each visit using dplyr

How to calculate ratio from two different pandas dataframe

How do I calculate the ratio of two values within a SQL group?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive