裁剪图像并无损合并回去

huangapple go评论70阅读模式
英文:

crop image and merge it back without quality loss

问题

我使用了 .getSubimage() 将图像裁剪成小块:

int width = image.getWidth();
int height = image.getHeight();
int c = 4;
int r = 4;
int pWidth = width / c;
int pHeight = height / r;
int x = 0;
int y = 0;

for (int i = 0; i < c; i++) {
    y = 0;
    
    for (int j = 0; j < r; j++) {
        if ((r - j) == 1 && ((c - i) == 1)) {

            BufferedImage SubImage = image.getSubimage(x, y, width - x, height - y);
            File outfile = new File(imageName + "/" + "subPic" + i + " " + j + " " + "jpeg");
            ImageIO.write(SubImage, "jpeg", outfile);
            y += pHeight;
        } else if ((r - j) == 1) {

            BufferedImage SubImage = image.getSubimage(x, y, pWidth, height - y);
            File outfile = new File(imageName + "/" + "subPic" + i + " " + j + " " + "jpeg");
            ImageIO.write(SubImage, "jpeg", outfile);
            y += pHeight;
        } else if ((c - i) == 1) {
            BufferedImage SubImage = image.getSubimage(x, y, width - x, pHeight);
            File outfile = new File(imageName + "/" + "subPic" + i + " " + j + " " + "jpeg");
            ImageIO.write(SubImage, "jpeg", outfile);
            y += pHeight;
        } else {
            BufferedImage SubImage = image.getSubimage(x, y, pWidth, pHeight);
            y += pHeight;
            File outfile = new File(imageName + "/" + "subPic" + i + " " + j + " " + "jpeg");
            ImageIO.write(SubImage, "jpeg", outfile);
        }
    }
    x += pWidth;
}

然后使用 g2d.drawimage() 将它们全部合并:

BufferedImage combinedIm = new BufferedImage(275, 183, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2d = combinedIm.createGraphics();

int currWidth = 0;
int currHeight = 0;
int iter = -1;

for (int i = 0; i < x; i++) {
    currHeight = 0;
    
    for (int j = 0; j < y; j++) {
        iter += 1;
        g2d.drawImage(imagePieces[iter], currWidth, currHeight, null);
        currHeight += imagePieces[iter].getHeight();
    }
    currWidth += imagePieces[iter].getWidth();
}
g2d.dispose();

这里的 imagePieces 是一个包含主图像子图的数组。但是合并后的图像质量较差,所以我的质量检查始终返回 false。

for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
        if (mainPic.getRGB(x, y) != subPic.getRGB(x, y)) {
            return false;

你可以尝试使用其他方法来裁剪和合并图像,以便通过相等性检查,或者可能有更好的方法来检查图像是否相同。

英文:

I cropped image into small pieces using .getSubimage()

int width = image.getWidth();
int height = image.getHeight();
int c = 4;
int r = 4;
int pWidth = width / c;
int pHeight = height / r;
int x = 0;
int y = 0;

for (int i = 0; i &lt; c; i++) {
    y = 0;
    
    for (int j = 0; j &lt; r; j++) {
        if ((r - j) == 1 &amp;&amp; ((c - i) == 1)) {

            BufferedImage SubImage = image.getSubimage(x, y, width - x, height - y);
            File outfile = new File(imageName + &quot;/&quot; + &quot;subPic&quot; + i + &quot; &quot; + j + &quot; &quot; + &quot;jpeg&quot;);
            ImageIO.write(SubImage, &quot;jpeg&quot;, outfile);
            y += pHeight;
        } else if ((r - j) == 1) {

            BufferedImage SubImage = image.getSubimage(x, y, pWidth, height - y);
            File outfile = new File(imageName + &quot;/&quot; + &quot;subPic&quot; + i + &quot; &quot; + j + &quot; &quot; + &quot;jpeg&quot;);
            ImageIO.write(SubImage, &quot;jpeg&quot;, outfile);
            y += pHeight;
        } else if ((c - i) == 1) {
            BufferedImage SubImage = image.getSubimage(x, y, width - x, pHeight);
            File outfile = new File(imageName + &quot;/&quot; + &quot;subPic&quot; + i + &quot; &quot; + j + &quot; &quot; + &quot;jpeg&quot;);
            ImageIO.write(SubImage, &quot;jpeg&quot;, outfile);
            y += pHeight;
        } else {
            BufferedImage SubImage = image.getSubimage(x, y, pWidth, pHeight);
            y += pHeight;
            File outfile = new File(imageName + &quot;/&quot; + &quot;subPic&quot; + i + &quot; &quot; + j + &quot; &quot; + &quot;jpeg&quot;);
            ImageIO.write(SubImage, &quot;jpeg&quot;, outfile);
        }
    }
    x += pWidth;
}

and then merged them all back using g2d.drawimage(),

BufferedImage combinedIm = new BufferedImage(275, 183, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2d = combinedIm.createGraphics();

int currWidth = 0;
int currHeight = 0;
int iter = -1;

for (int i = 0; i &lt; x; i++) {
    currHeight = 0;
    
    for (int j = 0; j &lt; y; j++) {
        iter += 1;
        g2d.drawImage(imagePieces[iter], currWidth, currHeight, null);
        currHeight += imagePieces[iter].getHeight();
    }
    currWidth += imagePieces[iter].getWidth();
}
g2d.dispose();

where imagePieces is an array with sub images of main image. But merged image has worse quality so my quality check always return false.

for (int x = 0; x &lt; width; x++) {
    for (int y = 0; y &lt; height; y++) {
        if (mainPic.getRGB(x, y) != subPic.getRGB(x, y)) {
            return false;

There is the original image:

裁剪图像并无损合并回去

and the merged image:

裁剪图像并无损合并回去

What else can I use to cut and merge image so it will pass equality check, or maybe there are better ways to check if images are the same.

答案1

得分: 0

我看到你正在对 .jpg 格式的图像进行压缩,因此你需要解码它们以获取 RGB 数据。我猜想你无法通过测试,可能是因为在压缩/解码图像碎片后造成了数据损失。尝试不要对图像碎片进行压缩,并将它们存储为 .bmp 格式。或者你可以修改测试:用不等式取代严格的比较,这样你就可以弥补前面提到的质量损失,例如:

final int max_diff = 3;
for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
        if (Math.abs(mainPic.getRGB(x, y) - subPic.getRGB(x, y)) > max_diff) {
            return false;
英文:

I see that you are compressing the pieces in .jpg, so you need to decode them back in order to obtain rgb data. I suppose that you couldn't pass the test because of the data loss after compressing/decoding pieces.
Try not to compress pieces and store them in .bmp format. Or you can modify your test: replace the strict comparison by the inequality, so you can compensate the aforementioned quality loss, e.g.:

final int max_diff = 3;
for (int x = 0; x &lt; width; x++) {
for (int y = 0; y &lt; height; y++) {
if (Math.abs(mainPic.getRGB(x, y) - subPic.getRGB(x, y)) &gt; max_diff) {
return false;

huangapple
  • 本文由 发表于 2020年9月20日 22:35:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63980080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定