Finding width and height of concave curved shaped blob

前端 未结 2 1262
星月不相逢
星月不相逢 2021-01-20 21:20

I have been scratching my head over this problem of calculating the width and height measurements of a figure like this. The major challenge is I cant use miBoundingrectangl

2条回答
  •  春和景丽
    2021-01-20 21:46

    This is an Imagemagick solution. But the concept will give you a clue how to proceed with OpenCV. It is not quite the dimensions you want. But the closest way I could conceive. Basically, it is the maximum inside bounding box. Perhaps there is something similar in OpenCV.

    Iterate around each edge of the image from outside towards the inside until each remaining edge is fully black with no white (or reasonably close to that).

    Input:

    convert img.png -threshold 0 -define trim:percent-background=0% -trim +repage -format "%wx%h" +write info: result.png
    
    returns: widthxheight => 144x317
    


    ADDITION:

    Here is another solution that should be easy to do in OpenCV.

    Trim to get the minimum outer bounding box. Extract the center row and column, which may have some white on the ends. Then pad with white all around. Then trim all the white again so you have one black row and column remaining with no white. Then get the dimensions of the single black row and single black column.

    width=`convert img.png -threshold 0 -trim +repage -gravity center -crop x1+0+0 +repage -bordercolor white -border 1x1 -trim +repage -format "%w" info:`
    height=`convert img.png -threshold 0 -trim +repage -gravity center -crop 1x+0+0 +repage -bordercolor white -border 1x1 -trim +repage -format "%h" info:`
    echo "width=$width; height=$height;"
    returns: => width=145; height=352;
    

提交回复
热议问题