问题描述
我正在进行图像处理以识别由石英样品组成的铁颗粒,为此,我必须识别浅灰色背景中的黑色斑点。我建立了一个java程序来做到这一点。然而,该程序没有识别图像中的黑点。请帮帮我。
/ *
*要更改此许可证标题,请在项目属性中选择许可证标题。
*要更改此模板文件,请选择工具|模板
*并在编辑器中打开模板。
* /
包图像处理;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
/ **
*
* @author My Kindom
* /
公共类imageprocessingf扩展javax.swing.JFrame {
/ **
*创建新表格imageprocessingf
* /
double sum = 0;
public imageprocessingf(){
initComponents();
}
public double imageprocessing1(){
try {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// BufferedImage image = ImageIO.read(新文件(C:\\Users \\ Myy Kindom \\Desktop \\printscreen.JPG));
BufferedImage image = ImageIO.read(新文件(C:\\Users \\Samarasinghe \\Downloads\\IS_1.jpg));
byte [] data =((DataBufferByte)image.getRaster()。getDataBuffer())。getData();
Mat mat = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC3);
mat.put(0,0,data);
Mat mat1 = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC3);
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);
ImageIO.write(image1,jpg,新文件(C:\\Users \\Samarasinghe \\Desktop\\\\\\\\\\\\\\
Mat source = Imgcodecs.imread(C:\\Users \\Samarasinghe\\Desktop\\hsv.jpg,Imgcodecs.CV_LOAD_IMAGE_COLOR);
Mat destination = new Mat(source.rows(),source.cols(),source.type());
destination = source;
标量lowerb =新标量(0,0,0);
Scalar upperb = new Scalar(180,255,30);
Core.inRange(source,lowerb,upperb,destination);
Imgcodecs.imwrite(C:\\Users\\Samarasinghe \\Desktop\\ThreshZero.jpg,目的地);
列表< MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(目的地,等高线,层次结构,Imgproc.RETR_EXTERNAL,Imgproc.CHAIN_APPROX_SIMPLE);
for(int j = 0; j< contours.size(); j ++){
double [] d = hierarchy.get(0,j);
Rect rect = Imgproc.boundingRect(contours.get(j));
Point pt1 = new Point(rect.x,rect.y);
Point pt2 = new Point(rect.x + rect.width,rect.y + rect.height);
Scalar eder = new Scalar(0,255,0);
Imgproc.rectangle(destination,pt1,pt2,eder,2);
Mat contour = contours.get(j);
double contourarea = Imgproc.contourArea(contour);
sum = sum + contourarea;
} System.out.println(Sum+ sum);
} catch(例外e){
}
返还金额;
};
/ **
*从构造函数中调用此方法以初始化表单。
*警告:请勿修改此代码。此方法的内容始终由表单编辑器重新生成
*。
* /
@SuppressWarnings(unchecked)
//< editor-fold defaultstate =collapseddesc =Generated Code>
private void initComponents(){
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText(jButton1);
jButton1.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane()。setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
.addContainerGap(272,Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(55,55,55))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
.addContainerGap(164,Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(113,113,113))
);
pack();
} //< / editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
imageprocessing1();
}
/ **
* @param args命令行参数
* /
public static void main(String args []){
/ *设置Nimbus外观* /
//< editor-fold defaultstate =collapseddesc =外观设置代码(可选)>
/ *如果Nimbus(在Java SE 6中引入)不可用,请使用默认的外观。
*有关详细信息,请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
* /
尝试{
for(javax.swing) .UIManager.LookAndFeelInfo info:javax.swing.UIManager.getInstalledLookAndFeels()){
if(Nimbus.equals(info.getName())){
javax.swing.UIManager.setLookAndFeel(info .getClassName());
休息;
}
}
} catch(ClassNotFoundException ex){
java.util.logging.Logger.getLogger(imageprocessingf.class.getName())。log(java.util。 logging.Level.SEVERE,null,ex);
} catch(InstantiationException ex){
java.util.logging.Logger.getLogger(imageprocessingf.class.getName())。log(java.util.logging.Level.SEVERE,null,ex) ;
} catch(IllegalAccessException ex){
java.util.logging.Logger.getLogger(imageprocessingf.class.getName())。log(java.util.logging.Level.SEVERE,null,ex) ;
} catch(javax.swing.UnsupportedLookAndFeelException ex){
java.util.logging.Logger.getLogger(imageprocessingf.class.getName())。log(java.util.logging.Level.SEVERE, null,ex);
}
//< / editor-fold>
/ *创建并显示表单* /
java.awt.EventQueue.invokeLater(new Runnable(){
public void run(){
new imageprocessingf()。setVisible (true);
}
});
}
//变量声明 - 不要修改
private javax.swing.JButton jButton1;
//变量结束声明
}
我猜您的图像在背景中有一些阴影,全局阈值不会给出预期的结果!
您可以尝试使用自适应阈值或
黑帽:
I am doing image processing to identify the iron particles which are consists on a quartz sample and to do that I have to identify black colour spots which are in light gray background. I built up a java program to do this. However the program did not identify black spots in image. Please help me on this.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package imageprocessing;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
/**
*
* @author My Kindom
*/
public class imageprocessingf extends javax.swing.JFrame {
/**
* Creates new form imageprocessingf
*/
double sum =0;
public imageprocessingf() {
initComponents();
}
public double imageprocessing1(){
try{
System.loadLibrary( Core.NATIVE_LIBRARY_NAME);
//BufferedImage image= ImageIO.read(new File("C:\\Users\\My Kindom\\Desktop\\printscreen.JPG"));
BufferedImage image= ImageIO.read(new File("C:\\Users\\Samarasinghe\\Downloads\\IS_1.jpg"));
byte[] data =((DataBufferByte) image.getRaster().getDataBuffer()).getData();
Mat mat = new Mat(image.getHeight(),image.getWidth(), CvType.CV_8UC3);
mat.put(0, 0, data);
Mat mat1 = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
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);
ImageIO.write(image1, "jpg", new File("C:\\Users\\Samarasinghe\\Desktop\\hsv.jpg"));
Mat source = Imgcodecs.imread("C:\\Users\\Samarasinghe\\Desktop\\hsv.jpg",Imgcodecs.CV_LOAD_IMAGE_COLOR);
Mat destination = new Mat(source.rows(),source.cols(),source.type());
destination = source;
Scalar lowerb=new Scalar (0,0,0);
Scalar upperb=new Scalar (180,255,30);
Core.inRange(source, lowerb, upperb, destination);
Imgcodecs.imwrite("C:\\Users\\Samarasinghe\\Desktop\\ThreshZero.jpg", destination);
List<MatOfPoint> contours= new ArrayList<>();
Mat hierarchy =new Mat();
Imgproc.findContours(destination, contours, hierarchy,Imgproc.RETR_EXTERNAL,Imgproc.CHAIN_APPROX_SIMPLE);
for(int j=0;j<contours.size();j++){
double[] d= hierarchy.get(0, j);
Rect rect = Imgproc.boundingRect(contours.get(j));
Point pt1=new Point(rect.x,rect.y);
Point pt2=new Point(rect.x+rect.width,rect.y+rect.height);
Scalar eder=new Scalar(0,255,0);
Imgproc.rectangle(destination, pt1, pt2, eder,2);
Mat contour = contours.get(j);
double contourarea=Imgproc.contourArea(contour);
sum = sum + contourarea;
}System.out.println("Sum"+sum);
}catch(Exception e){
}
return sum ;
};
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(272, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(55, 55, 55))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(164, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(113, 113, 113))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
imageprocessing1();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(imageprocessingf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(imageprocessingf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(imageprocessingf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(imageprocessingf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new imageprocessingf().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
I guess your images have some shading in the background and a global threshold would not give the intended results!
You can try to use an adaptive threshold or a top/bottom-hat transformation + a global threshold to segment the particles. So load your image as grayscale forget about Imgproc.cvtColor
and replace Core.InRange
with your segmentation method like threshold
, adaptiveThreshold
or others ;)
Here is some sample code in c++:
//Binarization using fixed threshold -> failed because of shading background
threshold(source,otsu, 0, max_value, THRESH_OTSU);
//Adaptive threshold
adaptiveThreshold(source, adaptive, 255, ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY, 25, 11);
//Segmentation with Black-Hat to uniform background
int const morph_size = 20;
Mat element = getStructuringElement(CV_SHAPE_ELLIPSE, Size(2 * morph_size + 1, 2 * morph_size + 1), Point(morph_size, morph_size));
morphologyEx(source, morph, MORPH_BLACKHAT, element);
threshold(morph, destination, 16, 255, THRESH_BINARY_INV);
Otsu:Adaptive:Black-Hat:
这篇关于如何使用opencv java识别黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!