poster
2019-12-14, 20:13
我在我的最后一年Engg.i希望对我的最后一年项目进行略微修改的草图生成算法。为此,我希望访问和修改像素强度值(不是RGB而是数字虽然该算法在matlab中工作得很好,但是由于项目的要求,我打算在Java中实现。通过互联网和各种Java论坛都没有帮助我。
Matlab代码允许我使用以下代码段访问像素
在120,234处的像素强度值由“ A(120,234)”给出,其中A是所考虑图像的名称。
同样,我想访问Java中图像的像素强度值并使用算法对其进行修改。
如果有人帮助我,我将非常高兴。
提前致谢
回答:
既然您可以使用Matlab,我建议您深入研究他们的代码,假设他们的图像是用Matlab编写的(我认为是这样),然后看看他们如何将RGB转换为强度。他们在使用HSL(色相-饱和度-亮度)吗?或其他一些颜色转换。知道了这一点,您可以找到Java代码来将RGB转换为HSL。
编辑:
根据对这个问题的评论,我认为这段代码将起作用。它不完整,因为我没有复制并重新编写所有操作,但是应该可以给您带来灵感。
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 < h; y++ ) { for ( int x = 0; x < w; x++ ) { int idx = ( y * w ) + x; int p = image[idx]; int r = p & 0x00FF0000 >> 16; int g = p & 0x0000FF >> 8; int b = p & 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 < h; y++ ) { for ( int x = 1; x + 1 < w; x++ ) { int idx = ( y * w ) + x; // Apply the convolution for ( int cy = 0; cy < convolutionSize; cy++ ) { for ( int cx = 0; cx < convolutionSize; cx++ ) { int cIdx = ( ( ( y - 1 ) + cy ) * w ) + ( ( x - 1 ) + cx ); newImage[idx] += convolution[cy][cx] * image[cIdx]; } } // pixel value rounding if ( newImage[idx] < 0 ) { newImage[idx] = -newImage[idx]; } else { newImage[idx] = 0; } if ( newImage[idx] > 0 ) { newImage[idx] = 120 - newImage[idx]; } else { newImage[idx] = 255; } } } // Convert to "proper" grayscale for ( int y = 0; y < h; y++ ) { for ( int x = 0; x < w; x++ ) { int idx = ( y * w ) + x; int p = newImage[idx]; newImage[idx] = 0xFF000000 | ( p
Matlab代码允许我使用以下代码段访问像素
在120,234处的像素强度值由“ A(120,234)”给出,其中A是所考虑图像的名称。
同样,我想访问Java中图像的像素强度值并使用算法对其进行修改。
如果有人帮助我,我将非常高兴。
提前致谢
回答:
既然您可以使用Matlab,我建议您深入研究他们的代码,假设他们的图像是用Matlab编写的(我认为是这样),然后看看他们如何将RGB转换为强度。他们在使用HSL(色相-饱和度-亮度)吗?或其他一些颜色转换。知道了这一点,您可以找到Java代码来将RGB转换为HSL。
编辑:
根据对这个问题的评论,我认为这段代码将起作用。它不完整,因为我没有复制并重新编写所有操作,但是应该可以给您带来灵感。
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 < h; y++ ) { for ( int x = 0; x < w; x++ ) { int idx = ( y * w ) + x; int p = image[idx]; int r = p & 0x00FF0000 >> 16; int g = p & 0x0000FF >> 8; int b = p & 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 < h; y++ ) { for ( int x = 1; x + 1 < w; x++ ) { int idx = ( y * w ) + x; // Apply the convolution for ( int cy = 0; cy < convolutionSize; cy++ ) { for ( int cx = 0; cx < convolutionSize; cx++ ) { int cIdx = ( ( ( y - 1 ) + cy ) * w ) + ( ( x - 1 ) + cx ); newImage[idx] += convolution[cy][cx] * image[cIdx]; } } // pixel value rounding if ( newImage[idx] < 0 ) { newImage[idx] = -newImage[idx]; } else { newImage[idx] = 0; } if ( newImage[idx] > 0 ) { newImage[idx] = 120 - newImage[idx]; } else { newImage[idx] = 255; } } } // Convert to "proper" grayscale for ( int y = 0; y < h; y++ ) { for ( int x = 0; x < w; x++ ) { int idx = ( y * w ) + x; int p = newImage[idx]; newImage[idx] = 0xFF000000 | ( p