Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何对2D图像执行某些操作?_Java_Invert - Fatal编程技术网

Java 如何对2D图像执行某些操作?

Java 如何对2D图像执行某些操作?,java,invert,Java,Invert,调用invert将图像从右向左向后翻转,即,它将第一列换成最后一列,第二列换成第二列,依此类推,直到整个图像反转为止 调用exchange将从行索引50和列索引60开始的宽度为180、高度为130的矩形定义的图像区域替换为从行索引50和列索引260开始的大小相同的矩形 调用shift查找隐藏在图像数据中的图像,方法是保存位7,将位0-6向左移动一个位置,并将位0设置为先前保存的位7的值 调用swap恢复一个图像,其中每个像素都已被置乱,方法是将底部的2位与顶部的2位分开。为此,您的代码需要执行相

调用invert将图像从右向左向后翻转,即,它将第一列换成最后一列,第二列换成第二列,依此类推,直到整个图像反转为止

调用exchange将从行索引50和列索引60开始的宽度为180、高度为130的矩形定义的图像区域替换为从行索引50和列索引260开始的大小相同的矩形

调用shift查找隐藏在图像数据中的图像,方法是保存位7,将位0-6向左移动一个位置,并将位0设置为先前保存的位7的值

调用swap恢复一个图像,其中每个像素都已被置乱,方法是将底部的2位与顶部的2位分开。为此,您的代码需要执行相同的交换来恢复映像

注意:一个像素(Picture.MAXVAL)的最大值是255,因此每个像素只有8位有效。这些是编号的位0-7,其中位0等于1,位7等于128。不允许有负值

我倒过来工作,但我倒过来工作,不是从右到左。有人能帮我怎么让它从右到左吗?比如我应该看哪一部分,或者如何修复

至于交换,我很接近,但不是很接近

交换和转移,我不知道怎么做

public class pictures {

// Picture object
Picture picture = null;

// Image data
int height;
int width;
int image[][];

// Constructor
public pictures() {
    picture = new Picture();
}

// Read the image
public void readImage(String inFile) {
    System.out.println("Reading image: " + inFile);
    try {
        picture.readPGM(inFile);
        height = picture.getHeight();
        width  = picture.getWidth();
        image  = picture.getData();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

// Write the image
public void writeImage(String outFile) {
    System.out.println("Writing image: " + outFile);
    try {
        picture.setData(image);
        picture.writePGM(outFile);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

// Get image data
public int[][] imageData() {
    return image;
}

// Invert the image
public void invert() {

    int right;
    int left;
    for (right = 0, left = height - 1; right < left; right++, left--) {
        int[] pic = image[left];
        image[left] = image[right];
        image[right] = pic;
    }
}

// Exchange the image
public void exchange() {

    for (int col = 60; col < 240; col++)
     {
        for (int row = 50; row < 180; row++) {

            int pic= image[row][col];
            image[row][col] = image[row][col+240];
            image[row][col+240] = pic;
        }
    }
}



// Swap 
public void swap() {

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {

            int pic = image[row][col];
            int top  = (pic & 0b11110000) >> 2;
            int bottom = (pic & 0b00001111) << 2;
            pic = top | bottom;
            image[row][col] = pic;
        }
    }
}

public void shift() {
公共类图片{
//图片对象
Picture=null;
//图像数据
内部高度;
整数宽度;
int图像[];
//建造师
公共图片(){
图片=新图片();
}
//阅读图片
公共void readImage(字符串填充){
System.out.println(“读取图像:+infle”);
试一试{
图片。readPGM(填充);
高度=picture.getHeight();
宽度=picture.getWidth();
image=picture.getData();
}捕获(例外e){
System.out.println(e.getMessage());
}
}
//写下图像
公共void writeImage(字符串输出文件){
System.out.println(“写入图像:+outFile”);
试一试{
图片。设置数据(图像);
picture.writePGM(输出文件);
}捕获(例外e){
System.out.println(e.getMessage());
}
}
//获取图像数据
公共int[][]图像数据(){
返回图像;
}
//反转图像
公共无效倒置(){
国际权利;
int左;
for(right=0,left=height-1;right>2;

int bottom=(pic&0b0001111)假设您的图像缓冲区为[row][col]然后,您的反转例程实际上是交换行,从而从上到下翻转。您需要做的是在每一行中循环,而不是交换列。根据您所做的数学计算,Exchange似乎是将第60列与第300列交换。看起来您应该只向列值添加200。