我正在尝试对通道进行预处理,以便我可以在之后使用通道检测。 所以我想选择一个带OpenCV的矩形ROI,但它只给我下面的裁剪Rect。我不知道如何将Rect复制到原始图像或者应该做什么。
我想在原始图像上使用我的Rect ROI,如下图所示。 非常感谢帮助,因为在互联网上没有太多的java支持
import org.opencv.core.*;
import org.opencv.videoio.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.util.concurrent.TimeUnit;
import javax.swing.*;
public class PreProcessing {
public static void main(String[] args) throws InterruptedException
{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
//Create new MAT object
Mat frame = new Mat();
//Create new VideoCapture object
VideoCapture camera = new VideoCapture("Car Clip NA.mov");
//Create new JFrame object
JFrame jframe = new JFrame();
//Inform jframe what to do in the event that you close the program
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create a new JLabel object vidpanel
JLabel vidPanel = new JLabel();
//assign vidPanel to jframe
jframe.setContentPane(vidPanel);
//set frame size
jframe.setSize(640, 360);
//make jframe visible
jframe.setVisible(true);
while (true) {
//If next video frame is available
if (camera.read(frame)) {
/Create new image icon object and convert Mat to Buffered Image
Rect roi = new Rect(0, 200, 640, 140);
Mat rectCrop = frame.submat(roi);
//Mat imageROI = new Mat(frame,roi);
Mat bw = new Mat();
Mat destination = new Mat();
Imgproc.GaussianBlur(rectCrop,destination ,new Size(9,9),0);
Imgproc.cvtColor(destination, bw, Imgproc.COLOR_RGBA2GRAY);
//bw.copyTo(rectCrop);
ImageIcon image = new ImageIcon(Mat2BufferedImage(bw));
Thread.sleep(20);
//Update the image in the vidPanel
vidPanel.setIcon(image);
//Update the vidPanel in the JFrame
vidPanel.repaint();
}
}
}
public static BufferedImage Mat2BufferedImage(Mat m) {
//Method converts a Mat to a Buffered Image
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);
return image;
}
}