You are using the commands slightly wrong.
Your first commands are correct:
Code: Select all
convert f3_f4.jpg -threshold 70% out.png
convert out.png -morphology smooth Octagon out.png
convert out.png -shave 10x10 -bordercolor white -border 10x10 out.png
convert out.png -gravity east -chop 130x0 -background white -gravity east -splice 130x0 out.png
But you should combine them into one long command for efficiency. If you kept them separate to review each step it would have been better to name them out1, out2 ...
One Command Equivalent:
Code: Select all
convert f3_f4.jpg -threshold 70% \
-morphology smooth Octagon \
-shave 10x10 -bordercolor white -border 10x10 \
-gravity east -chop 130x0 -background white -gravity east -splice 130x0 out.png
Next you should have negated the image so it was white on black background, so that it creates a proper polarity mask.
Then you need to use a slightly different -connected-components command to create a mask. Note I have added
-define connected-components:mean-color=true, so that it makes the out colors the same as out.png and not colored with graylevels equal to the ids. You do not want to use -define connected-components:keep=0; otherwise, you would only have id 0 and the rest of the image would be transparent. You want the image to be white and black only for a mask
Code: Select all
convert out.png \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=40000 \
-define connected-components:verbose=true \
-connected-components 4 mask.png
The report strangely shows a small area (416) id (31), that should not be listed. Seems like a bug to me.
Code: Select all
Objects (id: bounding-box centroid area mean-color):
0: 1700x2340+0+0 834.7,1175.6 3133346 gray(0)
15: 657x410+271+490 613.9,689.7 174785 gray(255)
12: 548x378+826+307 1124.7,476.3 142044 gray(255)
66: 509x382+351+1668 609.7,1858.3 135930 gray(255)
19: 551x350+860+736 1159.6,923.1 135762 gray(255)
71: 437x331+775+1871 1011.5,2043.3 108776 gray(255)
65: 467x317+830+1466 1076.7,1630.1 105779 gray(255)
9: 1479x138+51+128 794.2,176.7 41162 gray(255)
31: 33x29+65+1194 80.9,1207.8 416 gray(0)
However it is really gone and you can see that from a check of the mask areas:
Code: Select all
convert mask.png \
-define connected-components:verbose=true \
-connected-components 4 null:
Code: Select all
Objects (id: bounding-box centroid area mean-color):
0: 1700x2340+0+0 834.6,1175.6 3133762 gray(0)
3: 657x410+271+490 613.9,689.7 174785 gray(255)
2: 548x378+826+307 1124.7,476.3 142044 gray(255)
6: 509x382+351+1668 609.7,1858.3 135930 gray(255)
4: 551x350+860+736 1159.6,923.1 135762 gray(255)
7: 437x331+775+1871 1011.5,2043.3 108776 gray(255)
5: 467x317+830+1466 1076.7,1630.1 105779 gray(255)
1: 1479x138+51+128 794.2,176.7 41162 gray(255)
No area 416 in the above.
Next you use the mask to get your final image.
Code: Select all
convert f3_f4.jpg mask.png -alpha off -compose copy_opacity -composite result.png