Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you have access to matlab, I'd suggest digging into their code, assuming their image stuff is written in Matlab, which I think it is, and see how they convert RGB into intensity. Are they using HSL (Hue-Saturation-Luminance)? Or some other color conversion. Knowing that, you can find Java code to convert, for instance, RGB into HSL.</p> <p><strong>Edit:</strong><br> As per the comments on this question, I think this code will work. Its not complete as I didn't copy and re-write all the manipulation, but it should give you the idea.</p> <pre><code> import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class Convolution { public static void main( String[] args ) throws Exception { File inputFile = new File("apple.jpg"); BufferedImage bufferedImage = ImageIO.read(inputFile); int w = bufferedImage.getWidth(); int h = bufferedImage.getHeight(); System.out.println("w=" + w + ", h=" + h); // Get Pixels int[] image = new int[w * h]; bufferedImage.getRGB(0, 0, w, h, image, 0, w); // Convert to simple grayscale for ( int y = 0; y &lt; h; y++ ) { for ( int x = 0; x &lt; w; x++ ) { int idx = ( y * w ) + x; int p = image[idx]; int r = p &amp; 0x00FF0000 &gt;&gt; 16; int g = p &amp; 0x0000FF &gt;&gt; 8; int b = p &amp; 0x000000FF; image[idx] = (int) ( ( r + g + b ) / 3.0 ); } } int convolutionSize = 3; int[][] convolution = { { 0, -1, 0 }, { -1, 4, -1 }, { 0, -1, 0 } }; int[] newImage = new int[w * h]; // Apply the convolution to the whole image, note that we start at // 1 instead 0 zero to avoid out-of-bounds access for ( int y = 1; y + 1 &lt; h; y++ ) { for ( int x = 1; x + 1 &lt; w; x++ ) { int idx = ( y * w ) + x; // Apply the convolution for ( int cy = 0; cy &lt; convolutionSize; cy++ ) { for ( int cx = 0; cx &lt; convolutionSize; cx++ ) { int cIdx = ( ( ( y - 1 ) + cy ) * w ) + ( ( x - 1 ) + cx ); newImage[idx] += convolution[cy][cx] * image[cIdx]; } } // pixel value rounding if ( newImage[idx] &lt; 0 ) { newImage[idx] = -newImage[idx]; } else { newImage[idx] = 0; } if ( newImage[idx] &gt; 0 ) { newImage[idx] = 120 - newImage[idx]; } else { newImage[idx] = 255; } } } // Convert to "proper" grayscale for ( int y = 0; y &lt; h; y++ ) { for ( int x = 0; x &lt; w; x++ ) { int idx = ( y * w ) + x; int p = newImage[idx]; newImage[idx] = 0xFF000000 | ( p &lt;&lt; 16 ) | ( p &lt;&lt; 8 ) | p; } } // Set the image to have the new values; bufferedImage.setRGB(0, 0, w, h, newImage, 0, w); // Write the new image as a PNG to avoid lossey compression, // and its eaiser than trying to display an image in Java. ImageIO.write(bufferedImage, "png", new File("new_apple.png")); } } </code></pre> <p><strong>Edit:</strong><br> Modified the code to work as expected. Its not fast, but it works.<br> Before:<br> <img src="https://i.stack.imgur.com/4B2kD.jpg" alt="Before"></p> <p>After:<br> <img src="https://i.stack.imgur.com/7lEcC.png" alt="After"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload