0
我試圖將rgb圖像的顏色空間從RGB轉換爲HSV。我用下面的代碼:將圖像的顏色空間從RGB轉換爲HSV
public class Main {
public static void main(String[] args)
{
try{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
File input = new File("H:/Development/workspace/label.png");
BufferedImage image = ImageIO.read(input);
byte[] data=extractBytes(image);
Mat mat = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC2);
mat.put(0, 0, data);
Mat mat1 = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC2);
Imgproc.cvtColor(mat, mat1, Imgproc.COLOR_RGB2HSV);
byte[] data1 = new byte[mat1.rows()*mat1.cols()*(int)(mat1.elemSize())];
mat1.get(0, 0, data1);
BufferedImage image1 = new BufferedImage(mat1.cols(), mat1.rows(), 5);
image1.getRaster().setDataElements(0,0,mat1.cols(),mat1.rows(),data1);
File ouptut = new File("H:/Development/workspace/label_hsv.png");
ImageIO.write(image1, "png", ouptut);
} catch (Exception e) {
// System.out.println("Failed");
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
public static byte[] extractBytes (BufferedImage bufferedImage) throws IOException {
WritableRaster raster = bufferedImage .getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
return (data.getData());
}
}
但它失敗了一些錯誤:斷言失敗
OpenCV Error: Assertion failed ((scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F)) in cv::cvtColor, file ..\..\..\..\opencv\modules\imgproc\src\color.cpp, line 3959
CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\imgproc\src\color.cpp:3959: error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) in function cv::cvtColor
]
at org.opencv.imgproc.Imgproc.cvtColor_1(Native Method)
at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:4598)
at com.pradeep.exper.Main.main(Main.java:28)
任何幫助爲什麼它是怎麼回事?
CvType.CV_8UC2 //應該是CvType.CV_8UC3。另外,爲什麼不使用Highgui.imread(),並保存大量的位圖轉換? – berak 2014-10-03 08:37:39
你也只需要Mat mat1 = new Mat();爲cvtColor輸出 – berak 2014-10-03 08:38:42