4
$\begingroup$

Consider the following images with vertical grid lines.

enter image description here

They can be loaded in mathematica by the following code:

path="https://raw.githubusercontent.com/ccosnett/epc_parser/master/assets/bestiary/";
imgs = path <> # & /@ {
                        "102489965-2-ei-rating.png"
                      , "102492044-1-epc.jpg"
                      , "102519884-1-epc-graphs.jpg"
                      , "102729176-2-eir.jpeg"
                      , "102984668-1-epc-rating-graph.png"
                      , "103003823-1-epc.gif"
                      , "103187228-4-epc-rating-graph.png"
                      , "103309151-1-ee-rating.png"
                      , "105825026-1-epc-1.png"
                      , "105825038-1-epc-1.jpeg"
                      , "105829613-1-epc-1.png"
                      , "105835280-1-epc-1.jpeg"
                      , "108300254-1-epc-rating-graph.png"
                      , "108386993-1-environment-impact.jpg"
                      , "108386993-2-energy-efficiency.jpg"
                      , "test-1-12.png"
                      , "test-81-29.png"
                      , "test-88-1.png"};
images = Import /@ imgs

Challenge:

write a function which returns the vertical grid lines on the right hand side of the images.

enter image description here

Attempt 1:

I tried to preprocess my images to make it easier for ImageLines. I made all the images grayscale and all the lines white on black.

Clear[verticalLines];
verticalLines[im_] := Module[{imag, lines, vertLines},
                              imag = ColorNegate[ColorConvert[im, "Grayscale"]];
                              lines = ImageLines[imag];
                              vertLines = Cases[lines, Line[{{x1_, _}, {x2_, _}}] /; Abs[x1 - x2 ] < 2];
                              HighlightImage[imag, vertLines ]
                      ];
verticalLines /@ images

enter image description here

Unfortunately this is not a robust method because it misses many of the vertical lines.

How can I detect vertical lines in a more robust manner?

Any help would be really appreciated. Thanks in advance!

$\endgroup$
2
  • 1
    $\begingroup$ This preprocessing seems more stable Binarize[GaussianFilter[ColorConvert[im,"Grayscale"],1,{0,1}],0.05] $\endgroup$
    – swish
    Commented Aug 6, 2021 at 0:03
  • $\begingroup$ Did you try using the second and/or third parameters of ImageLines? These provide some flexibility in what is detected. $\endgroup$
    – bill s
    Commented May 28, 2023 at 14:59

2 Answers 2

1
$\begingroup$
jj = Rasterize[fig, RasterSize -> 200];
kk = ImageAdjust[
   Rasterize[ImageTake[jj, {130, 245}, {60, 182}], 
    RasterSize -> 300], {.01, .01}];
ImageAdjust@
 ImageConvolve[Erosion[MorphologicalBinarize[kk, {.8, .9}], 4], 
  50 {{-2, 0, 0, 0, 2}, {-2, 0, 0, 0, 2}, {-2, 0, 0, 0, 2}, {-2, 0, 0,
      0, 2}, {-2, 0, 0, 0, 2}}]

enter image description here

$\endgroup$
1
$\begingroup$

I guess, the simplest way is to use the built-in function ImageLines

i = 4;
HighlightImage[images[[i]], ImageLines[EdgeDetect[images[[i]], 0.6], 0.4]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.