6

I want to thin handwritten characters like shown below:

enter image description here

Code below give my expected result:

BW = imread('s.png');
BWI = imcomplement(BW);
BW2D = im2bw(BWI,0.1);
BWT = bwmorph(BW2D,'thin',Inf),
BWFinal = imcomplement(BWT);
figure, imshow(BWFinal);

Is this the correct approach? Or is there another way to do it in MATLAB?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
JR Galia
  • 17,229
  • 19
  • 92
  • 144
  • 8
    if it gives you the expected result - it is likely to be a correct approach. – Shai Feb 09 '15 at 07:10
  • 4
    I wish all the questions on SO could be asnwered with `YES`, like this one. – Ander Biguri Feb 09 '15 at 08:38
  • 3
    @AnderBiguri - My answer is in honour of you! – rayryeng Feb 09 '15 at 14:08
  • 2
    @rayryeng hahah Excellent! – Ander Biguri Feb 09 '15 at 14:17
  • 2
    Well, you already *know* it is correct. *How* should any other approach be different? Performance? Readability? Shorter? More robust? – runDOSrun Feb 11 '15 at 20:57
  • 1
    I'll build on what runDOSrun is talking about. I was debating on putting this as an amendment to my answer, but because of its succinctness and its beauty, I'm going to leave it. MATLAB's morphological functions are highly optimized and use Intel Integrated Performance Primitives (IIPP) if available and if your processor supports it. With these highly optimized functions, I doubt you will get anything faster. In addition, what you're doing is the standard way of skeletonizing a binary object. There isn't really much more to be done other than adaptively setting your threshold. – rayryeng Feb 11 '15 at 23:33
  • 2
    Actually, you are not creating a skeleton. You create a thinned version of the letter. But I assume that is what you actually need, as I don't think the skeleton would be of much use in letter recognition. – hbaderts Feb 12 '15 at 07:21
  • @hbaderts - Very true. When I said "skeletonize", I actually meant thin. I got my terminology mixed up. I edited the post to get rid of the skeletonization because that's actually not being done in the OP's post. Thinning is all that is needed. – rayryeng Feb 12 '15 at 16:48
  • 1
    @hbaderts I think skeletons would help in letter recognition. In the right image you still see some dirt. All you want in the end is the skeleton, or am I wrong? The question is not really good. The asker could specify what else he really wants as alternative (faster, more accurate). As it is one could see it as showing off. ;) – NoDataDumpNoContribution Feb 14 '15 at 21:50
  • @Trilarion - Most of the reason for my answer is because I interpreted the OP as showing off and my succinctness is motivated by that fact. If he wanted an evaluation on his code, he should have posted on Code Review. BTW, your statement on the skeleton is certainly correct, but the shape may not be recognizable. As such, there is a fine line between how many times you are thinning in comparison to letting the contour become fully thin in order to be a skeleton. Still, great observation! – rayryeng Feb 17 '15 at 15:27

2 Answers2

12

Yes.‏‏‏ ‏‏ ‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏‏

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • 6
    Your most enlightening answer yet. ;) – eigenchris Feb 11 '15 at 20:24
  • 2
    @eigenchris - lol thanks. Someone didn't agree and gave me a downvote, but I put this answer for a demonstration more or less. The minimum character count you need to write an answer is 30, but I used invisible separators to buff up the character count. – rayryeng Feb 11 '15 at 20:30
  • 2
    @rayryeng the shortest answer to get a bounty :P :P – roni Feb 12 '15 at 16:40
  • 1
    @roni - That would certainly be one for the record books if this is the case. TBH, I'm not sure why a bounty was issued lol, but if I do end up getting it, that would be very funny. – rayryeng Feb 12 '15 at 16:48
  • 2
    @rayryeng I hope you'll appreciate the humor ;) – Shai Feb 15 '15 at 06:45
  • @rayryeng this is certainly an exemplary answer! Your easiest 100 reps ever ;) – Shai Feb 17 '15 at 06:29
  • @Shai - lmao. thank you so much. you actually didn't have to but thank you! – rayryeng Feb 17 '15 at 06:30
  • @rayryeng apparently SO mods [lack sense of humor](http://stackoverflow.com/a/34626953/1714410)... Too bad. – Shai Jan 07 '16 at 06:15
  • apparently SO does not like this kind of answers: https://meta.stackoverflow.com/q/364977/1714410 – Shai Mar 23 '18 at 05:58
  • 1
    @Shai Blah... well it looks like we got our answers in before they hated them. – rayryeng Mar 23 '18 at 06:09
  • @Shai Since I have you here, you have a deleted answer on this thread that finds the pairwise Euclidean distance between two sets of matrices: https://stackoverflow.com/questions/23911670/efficiently-compute-pairwise-squared-euclidean-distance-in-matlab. As of R2016b, the accepted answer is slower. The method you proposed with broadcasting and matrix multiplication with my tests is 5 times faster. I would consider undeleting it as I am currently using that in my work. I'll also give you a vote too. – rayryeng Mar 23 '18 at 20:45
  • @rayryeng thanks for your comment. I undeleted that post. – Shai Mar 25 '18 at 08:08
6

The consensus is that your code is ok. However, to give Shai some mileage on his points, I add a minor comment:

The use of imcomplement may not be necessary, see the documentation.

In particular:

Tip If IM is a grayscale or RGB image of class double, you can use the expression 1-IM instead of this function.

If IM is a binary image, you can use the expression ~IM instead of this function.

Buck Thorn
  • 5,024
  • 2
  • 17
  • 27