Ask Your Question

how to calculate the horizontal width of chest in a binary body picture??

asked 2015-11-08 20:24:23 -0500

situs gravatar image

updated 2015-11-10 02:13:41 -0500

below is the source pic:

image description

below is my binary pic after processing on android platform:

image description

now i want the distance between the two red lines (red lines are added by my mspainter).

would anyone like to give me some tips ? that would be very thankful !

==================update at 2015-11-10 11:11 =========================

after eroded 5 times and dilated 5 times i got a better pic below , it costs some details (like armpit) ,but sort of could being accepted yet.

image description

==================final update at 2015-11-10 11:11======================

at last, i choose to scan pixels finally...

scan from top to the horizon red line at chest, they all have two "transfer points" (as the red squares in the pic) , and below chest red line(include chest line) there are 6 "transfer points"(as the blue ones). image description

    int count = 0;//counting the number of "transfer points"
    int[] data = new int[6];//storing the horizontal ordinate of each "transfer points"
    for (int i = 0; i < height; i += 5) {
        for (int j = 1; j < width; j++) {
            // val val2 ##########
            int val = (int) src.get(i, j - 1)[0];
            int val2 = (int) src.get(i, j)[0];
            if (val2 != val) {
                //find out the "transfer point",and count++
                data[count] = j;
                count += 1;
        if (count == 6) {
            //if count==6 ,
            // it means the line now being scanned is the chest line (or beyond the chest line)
            // otherwise it should be 2 "transfer point"  instead of 6
        } else {
            //re-count if less than 6 "transfer points"
            count = 0;
edit retag flag offensive close merge delete


Same problem in this post

LBerger gravatar imageLBerger ( 2015-11-09 02:49:13 -0500 )edit

@situs, ill formulated problem. the distance between 2 points is an easy euclidean distance, formulated in answer below.

what you're asking for is something completely different, like: "how do i seperate the human corso from arms/legs, and then get the horizontal width of that"

berak gravatar imageberak ( 2015-11-09 10:09:11 -0500 )edit

@berak, ok , thank you for point out my confusing description, ill fix it as you suggested. Thank you!

situs gravatar imagesitus ( 2015-11-09 10:33:33 -0500 )edit

@LBerger,thank you for reply,ill check it out..

situs gravatar imagesitus ( 2015-11-09 10:39:43 -0500 )edit

You can filter your image because background is not clean. After you can threshold and look for contour and use approxPolyDP. Here you are lucky two points of this approximation are near chest as you can see here

LBerger gravatar imageLBerger ( 2015-11-09 11:43:50 -0500 )edit

2 answers

Sort by » oldest newest most voted

answered 2015-11-09 11:18:38 -0500

as you said "would anyone like to give me some tips ? " i want to give some tips with the code below.

the idea is counting non-zero pixels for every column after thresholding the image .

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;

int main( int argc, char** argv )
    char* filename = argc >= 2 ? argv[1] : (char*)"14470355785103673.jpg";
    Mat img = imread(filename,0);
    if (img.empty())
        return -1;
    Mat m = img.clone();
    Mat bw;


    Mat kernel = Mat::ones(2, 2, CV_8UC1);
    erode(bw, bw, kernel);

    for ( int i = 0; i < bw.cols; i++)
        int nonzero = countNonZero(bw.col(i));

    imshow("result", img);
    return 0;

this result image shows non-zero pixels count ( you can analyze them and will find your desired points )

image description

edit flag offensive delete link more


ok , it is indeed a new angle..ill along this way. thank you for your reply~ :)

situs gravatar imagesitus ( 2015-11-09 21:40:14 -0500 )edit

answered 2015-11-09 09:31:12 -0500

chr0x gravatar image

updated 2015-11-09 09:34:35 -0500

Just subtract left point coordinates from right point coordinates...

If right point is on (200,100) and left point is on (100, 100), the distance will be 100 in x and 0 in y...

If you want only one value (and not x and y coordinates), use Euclidian distance:

distance = sqrt(deltaX^2 + deltaY^2)

Using (200,100) and (100,100) again, the distance will be:

distance = sqrt((200-100)^ 2 + (100-100)^2) ... distance = sqrt(100ˆ2) ... distance = 10

edit flag offensive delete link more


ok..thank you for this reply ..but it is not a simple calculate question... let me ask you that how do you know the right point is (200,100) ? scan the pixels ? and how could you know your scan stop point -- i.e. when you stop scanning ? that is the problem.

situs gravatar imagesitus ( 2015-11-09 09:40:52 -0500 )edit

just saying, opencv is mainly a c++ shop, and ^ operator is not "pow()" but "xor"

berak gravatar imageberak ( 2015-11-09 10:11:15 -0500 )edit

Question Tools

1 follower


Asked: 2015-11-08 20:24:23 -0500

Seen: 770 times

Last updated: Nov 10 '15