我正在使用带有Eclipse的opencv 3.1来分割更多图像中的图像。 问题是,当我尝试保存它们时,只保存9个图像中的第一个(例如)。 奇怪的是,控制台说图像已保存,但我只能看到一个。 代码如下:
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Range;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class prova {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
Imgcodecs imageCodecs = new Imgcodecs();
String file
="C:/Users/Zast/Desktop/workspacePR2/tesina/images/img.jpg";
Mat mat= imageCodecs.imread(file);
System.out.println("Image Loaded");
// Creating an empty matrix to store the result
// Mat dst = new Mat();
for (int i=0; i<3; i++){
for (int j=0; j<3; j++){
int a1=i*200;
int b1=i*200+199;
int a2=j*200;
int b2=j*200+199;
Mat mat2= mat.submat(a1,b1,a2,b2);
imshow ("filtro", mat2);
String file2 =
"C:/Users/Zast/Desktop/workspacePR2/tesina/images/img1.jpg";
imageCodecs.imwrite(file2, mat2);
System.out.println("Image Saved ............");
}}
}
private static String String(int i) {
return null;
}
public static void imshow(String windowname, Mat m){
int type = BufferedImage.TYPE_BYTE_GRAY;
if ( m.channels() > 1 )
type = BufferedImage.TYPE_3BYTE_BGR;
int bufferSize = m.channels()*m.cols()*m.rows();
byte [] b = new byte[bufferSize];
m.get(0,0,b); // get all the pixels
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
final byte[] targetPixels = ((DataBufferByte)
image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);
ImageIcon icon=new ImageIcon(image);
JFrame frame=new JFrame(windowname);
JLabel lbl=new JLabel(icon);
frame.add(lbl);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println("Image Processed");
} }
输出控制台如下:
Image Loaded
Image Processed
Image Saved ............
Image Processed
Image Saved ............
Image Processed
Image Saved ............
Image Processed
Image Saved ............
Image Processed
Image Saved ............
Image Processed
Image Saved ............
Image Processed
Image Saved ............
Image Processed
Image Saved ............
Image Processed
Image Saved ............