gpt4 book ai didi

java - 跳过像素问题

转载 作者:行者123 更新时间:2023-12-01 05:00:53 24 4
gpt4 key购买 nike

我正在编写一个程序,它将获取二维码图像并识别黑/白像素并将它们保存在数组中。但我的问题是;我有 294x294 像素图像和一个固定大小 21x21 的矩阵。所以一个方形模块对我来说是 294/21=4.0 像素。如何按 4.0 像素到 4.0 像素搜索像素?这是我的代码:

public static void printAllARGBDetails(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
System.out.println("Image Dimension: Height-" + height + ", Width-"
+ width);
System.out.println("Total Pixels: " + (height * width));
int pixels[][]=new int[21][21];
int pixel=width/21;
System.out.println("Pixel=" +pixel);

for (int i = 20; i>=0 ; i--) {
for (int j = 20; j>=0 ; j--) {
if(image.getRGB(i, j)==-1)
{

pixels[i][j]=0;
}
else
{
pixels[i][j]=1;
}

}
}
}

`

最佳答案

您可能应该引入一种方法,以某种方式将 4x4 像素数组计算为 01,例如基于平均光度:

public static void printAllARGBDetails(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
System.out.println("Image Dimension: Height-" + height + ", Width-"
+ width);
System.out.println("Total Pixels: " + (height * width));
int pixels[][]=new int[21][21];
int pixel=width/21;
System.out.println("Pixel=" +pixel);

for (int i = 20; i>=0 ; i--) {
for (int j = 20; j>=0 ; j--) {
pixels[i][j] = getPixel (image, i, j);
System.out.println(String.format ("Pixel[%d,%d]=%d", i, j, pixels[i][j]));
}
}
}

private static final long LUMINOSITY_THRESHOLD = 23000;

private static int getPixel (BufferedImage image, int i, int j) {
long totalLuminosity = 0;
for (int k = 0; k < 4; k++) {
for (int m = 0; m < 4; m++) {
long luminosity = getLuminosity (image.getRGB (i*4 + k, j*4 + m));
totalLuminosity += luminosity;
}
}

return totalLuminosity/16 > LUMINOSITY_THRESHOLD ? 0 : 1;
}

private static int getLuminosity (int rgb) {
int r = rgb >> 16;
int g = (rgb >> 8) & 0xFF;
int b = rgb & 0xFF;

int luminosity = 21*r + 71*g + 7*b;

return luminosity;
}

关于java - 跳过像素问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13383301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com