Finding vertical stipes to use for crop input

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
TedBaker
Posts: 36
Joined: 2017-10-10T14:14:55-07:00
Authentication code: 1151

Finding vertical stipes to use for crop input

Post by TedBaker » 2017-10-30T15:47:44-07:00

I have images of films strips from a scanner, like the following example. These, that have been nicely trimmed.

https://ibb.co/dWDTbR

I want to find the horizontal coordinates of the separating strips, i.e. the continues grey areas separating each frame. I want to use the horizontal coordinates to drive a cropping operation. That way I can carefully control the cropping with my own logic.

I have a reasonable estimate of the 'colour' of each vertical spacing gap, in this case gray(35%). So I am looking for the horizontal coordinates of continuous area that matches the said colour with fuzzy match of 5% or so. Ideally of would like to know the start and end range of those coordinates.

Any clues?

Any help appreciated.

IM 6.8.9 linux

User avatar
fmw42
Posts: 22080
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Finding vertical stipes to use for crop input

Post by fmw42 » 2017-10-30T16:02:05-07:00

Is this not just a repost of viewtopic.php?f=1&t=32821

TedBaker
Posts: 36
Joined: 2017-10-10T14:14:55-07:00
Authentication code: 1151

Re: Finding vertical stripes to use for crop input

Post by TedBaker » 2017-10-30T16:06:14-07:00

fmw42 wrote:
2017-10-30T16:02:05-07:00
Is this not just a repost of viewtopic.php?f=1&t=32821
No the problem is more specific, hence I thought it best to start a new topic.

snibgo
Posts: 9378
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Finding vertical stipes to use for crop input

Post by snibgo » 2017-10-30T16:32:14-07:00

You can follow the approach Fred gave in today's thread viewtopic.php?f=22&t=32928, but horizontally instead of vertically. That is, scale to a single row, and auto-level. If you hadn't whited out portions, the margins would be the lightest portions of the image.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 22080
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Finding vertical stipes to use for crop input

Post by fmw42 » 2017-10-30T16:34:08-07:00

The problem is that you have shades of gray elsewhere of the same value. I can get it close, but there may be false positives that you will have to filter by looking at the regularity of the gaps.

The process is to

Replace near white with black to remove the white blobs so they do not average with the rest to similar shades of gray
Average the image to one row and then expand it again.
Replace your gap color with white and everything else with black
Use morphology to remove stripes of 1 pixel width as a precaution
Use connected components to the find the bounding boxes of all separate white regions.

Code: Select all

dim=`convert test.jpg -format "%wx%h" info:`
convert test.jpg -fuzz 25% -fill black -opaque white tmp0.png
convert tmp0.png -scale x1! -scale $dim! tmp1.png
convert tmp1.png -fuzz 2% -fill white -opaque "gray(90)" -fill black +opaque white tmp2.png
convert tmp2.png -morphology open "5x1:0,1,1,1,0" tmp3.png
arr=()
OLD_IFS=$IFS
IFS=$'\n'
arr=(`convert tmp3.png -type bilevel \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-connected-components 4 \
null: | sed 's/^[ ]*//' | tail -n +2`)
IFS=$OLD_IFS
num=${#arr[*]}
echo "num=$num"
for ((i=0;i<num;i++)); do
bbox=`echo "${arr[$i]}" | cut -d\  -f2`
color=`echo "${arr[$i]}" | cut -d\  -f5`
if [ "$color" = "gray(255)" ]; then
echo "bbox=$bbox;"
fi
done
original:
Image

tmp0 -- remove white blobs
Image

tmp1 -- average to one row and expand
Image

tmp2 -- convert to black and white
Image

tmp2 -- use morphology to remove 1 pixel wide stripes (not needed this time)
Image

result:

Code: Select all

bbox=10x94+716+0;
bbox=10x94+716+0;
bbox=8x94+868+0;
bbox=7x94+277+0;
bbox=6x94+130+0;
bbox=5x94+570+0;
bbox=5x94+424+0;

TedBaker
Posts: 36
Joined: 2017-10-10T14:14:55-07:00
Authentication code: 1151

Re: Finding vertical stipes to use for crop input

Post by TedBaker » 2017-10-30T16:50:49-07:00

fmw42 wrote:
2017-10-30T16:34:08-07:00
I can get it close, but there may be false positives that you will have to filter by looking at the regularity of the gaps.
Ok that's absolutely amazing, or should I say absolutely magic! The false positives should not a problem, to filter out as I can filter those out against the known aspect ratio of the frames, which is why I wanted to do it this way.

Thanks again!
Last edited by TedBaker on 2017-10-30T16:54:47-07:00, edited 1 time in total.

Post Reply