πŸš€ Edge Detection with Threads and MiniMagick in Ruby πŸŒ„

Digital image processing is a fascinating field where computers are used to transform, enhance, and interpret images. It plays a crucial role in numerous applications, from medical imaging to computer vision and machine learning. One of the foundationa…


This content originally appeared on DEV Community and was authored by Ernane Ferreira

Digital image processing is a fascinating field where computers are used to transform, enhance, and interpret images. It plays a crucial role in numerous applications, from medical imaging to computer vision and machine learning. One of the foundational techniques in this domain is edge detection βœ¨β€” a method used to identify significant transitions in pixel intensities, enabling us to detect objects and boundaries within images.

πŸ” Why Edge Detection Matters

Edges are essentially where the most critical information of an image lies. They represent boundaries between different objects, colors, or textures, making them vital for tasks like segmentation and object recognition. Without edge detection, computers would struggle to differentiate between regions in an image.

πŸ”¨ How Edge Detection Works

Edge detection is primarily achieved through mathematical operations called convolutions. By applying convolution filters, we can detect changes in pixel intensities in both the horizontal ( xx x ) and vertical ( yy y ) directions. The most commonly used filters for edge detection are the Prewitt, Sobel, and Canny operators. In this project, we focus on using the Prewitt operator to perform this task.

Consider the following convolution matrices for detecting edges in both directions:

Gx = |-1  0  1|    Gy = |-1 -1 -1|
     |-1  0  1|        |  0  0  0|
     |-1  0  1|        |  1  1  1|
  • GxG_x Gx​ detects horizontal changes.
  • GyG_y Gy​ detects vertical changes.

By applying these matrices across the image, we extract valuable edge information for each pixel. The final edge map is generated by combining both directional components.

πŸ“‹ Step-by-Step Process

Let's walk through the basic algorithm to detect edges in a grayscale image of size MΓ—NM \times N MΓ—N :

  1. Initialize Matrices:

    • Create three matrices: GxG_x Gx​ , GyG_y Gy​ , and GG G , each of size MΓ—NM \times N MΓ—N . These will store the horizontal, vertical, and final edge information, respectively.
  2. Convolution Operation:

    • For each pixel, the gradient in the xx x -direction ( GxG_x Gx​ ) and yy y -direction ( GyG_y Gy​ ) is calculated using the convolution matrices.
  3. Combine Results:

    • Finally, the gradient magnitudes are combined to form the final edge-detected image, GG G .
For i from 1 to M-2
  For j from 1 to N-2
    Gx(i, j) = [ I(i+1, j-1) + I(i+1, j) + I(i+1, j+1) ] - [ I(i-1, j-1) + I(i-1, j) + I(i-1, j+1) ]
    If Gx(i, j) < 0, Gx(i, j) = 0
    If Gx(i, j) > 255, Gx(i, j) = 255
  end-for
end-for

For i from 0 to M-1
  For j from 0 to N-1
    G(i, j) = Gx(i, j) + Gy(i, j)
    If G(i, j) > 255, G(i, j) = 255
  end-for
end-for

🧡 Multi-Threaded Implementation in Ruby

A key feature of this project is its use of multi-threading to parallelize the edge detection process πŸ§‘β€πŸ’». Instead of processing the entire image in a single thread, the work is divided into two threads:

  • One thread calculates the horizontal edges using GxG_x Gx​ .
  • Another thread handles the vertical edges with GyG_y Gy​ .

This approach significantly speeds up the computation, especially for large images.

  • Main thread: Reads the input image, initializes matrices, and spawns two threads.
  • Thread 1: Computes the edge detection in the xx x -direction.
  • Thread 2: Computes the edge detection in the yy y -direction.
  • Final step: The results from both threads are combined to produce the final edge map, GG G .

πŸ–ΌοΈ Results

Here are examples of processed images using this method. The left column shows the original images, and the right column presents the edge-detected outputs:

Original Processed
Coins Coins
Dog Dog
Lena Lena
UFRN UFRN

🎯 Conclusion

By leveraging the power of Ruby threads, this project demonstrates how to efficiently apply edge detection to images in both the horizontal ( xx x ) and vertical ( yy y ) directions. The results show how edge detection enhances the structural details of images, making them more suitable for tasks like object recognition and segmentation. This approach not only speeds up the processing but also makes it scalable for larger datasets.

For more details and to explore the code, visit the GitHub repository. πŸŽ‰


This content originally appeared on DEV Community and was authored by Ernane Ferreira


Print Share Comment Cite Upload Translate Updates
APA

Ernane Ferreira | Sciencx (2024-09-12T01:53:17+00:00) πŸš€ Edge Detection with Threads and MiniMagick in Ruby πŸŒ„. Retrieved from https://www.scien.cx/2024/09/12/%f0%9f%9a%80-edge-detection-with-threads-and-minimagick-in-ruby-%f0%9f%8c%84/

MLA
" » πŸš€ Edge Detection with Threads and MiniMagick in Ruby πŸŒ„." Ernane Ferreira | Sciencx - Thursday September 12, 2024, https://www.scien.cx/2024/09/12/%f0%9f%9a%80-edge-detection-with-threads-and-minimagick-in-ruby-%f0%9f%8c%84/
HARVARD
Ernane Ferreira | Sciencx Thursday September 12, 2024 » πŸš€ Edge Detection with Threads and MiniMagick in Ruby πŸŒ„., viewed ,<https://www.scien.cx/2024/09/12/%f0%9f%9a%80-edge-detection-with-threads-and-minimagick-in-ruby-%f0%9f%8c%84/>
VANCOUVER
Ernane Ferreira | Sciencx - » πŸš€ Edge Detection with Threads and MiniMagick in Ruby πŸŒ„. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/12/%f0%9f%9a%80-edge-detection-with-threads-and-minimagick-in-ruby-%f0%9f%8c%84/
CHICAGO
" » πŸš€ Edge Detection with Threads and MiniMagick in Ruby πŸŒ„." Ernane Ferreira | Sciencx - Accessed . https://www.scien.cx/2024/09/12/%f0%9f%9a%80-edge-detection-with-threads-and-minimagick-in-ruby-%f0%9f%8c%84/
IEEE
" » πŸš€ Edge Detection with Threads and MiniMagick in Ruby πŸŒ„." Ernane Ferreira | Sciencx [Online]. Available: https://www.scien.cx/2024/09/12/%f0%9f%9a%80-edge-detection-with-threads-and-minimagick-in-ruby-%f0%9f%8c%84/. [Accessed: ]
rf:citation
» πŸš€ Edge Detection with Threads and MiniMagick in Ruby πŸŒ„ | Ernane Ferreira | Sciencx | https://www.scien.cx/2024/09/12/%f0%9f%9a%80-edge-detection-with-threads-and-minimagick-in-ruby-%f0%9f%8c%84/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.