本文整理汇总了Java中org.opencv.imgcodecs.Imgcodecs类的典型用法代码示例。如果您正苦于以下问题:Java Imgcodecs类的具体用法?Java Imgcodecs怎么用?Java Imgcodecs使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Imgcodecs类属于org.opencv.imgcodecs包,在下文中一共展示了Imgcodecs类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: showResult
import org.opencv.imgcodecs.Imgcodecs; //导入依赖的package包/类
public void showResult(Mat img) {
Imgproc.resize(img, img, new Size(640, 480));
MatOfByte matOfByte = new MatOfByte();
Imgcodecs.imencode(".jpg", img, matOfByte);
byte[] byteArray = matOfByte.toArray();
BufferedImage bufImage = null;
try {
InputStream in = new ByteArrayInputStream(byteArray);
bufImage = ImageIO.read(in);
JFrame frame = new JFrame();
frame.getContentPane().add(new JLabel(new ImageIcon(bufImage)));
frame.pack();
frame.setVisible(true);
} catch (IOException | HeadlessException e) {
e.printStackTrace();
}
}
示例2: denoise
import org.opencv.imgcodecs.Imgcodecs; //导入依赖的package包/类
public static void denoise() {
String imgInPath = "captchaExample.jpg";
imgInPath = "MyCaptcha.PNG";
imgInPath = "blurredtext.jpg";
String imgOutPath = "captchaNoiseRemovedExample.png";
imgOutPath = "MyNoiseRemovedCaptcha.PNG";
Mat image = Imgcodecs.imread(imgInPath);
Mat out = new Mat();
Mat tmp = new Mat();
Mat kernel = new Mat(new Size(3, 3), CvType.CV_8UC1, new Scalar(255));
// Mat kernel = new Mat(image.size(), CvType.CV_8UC1, new Scalar(255));
Imgproc.morphologyEx(image, tmp, Imgproc.MORPH_OPEN, kernel);
Imgproc.morphologyEx(tmp, out, Imgproc.MORPH_CLOSE, kernel);
Imgcodecs.imwrite(imgOutPath, out);
}
示例3: findImage
import org.opencv.imgcodecs.Imgcodecs; //导入依赖的package包/类
@Override
public ImageFinderResult findImage(Rectangle sourceScreenRect, File templateImage, double desiredAccuracy) {
try {
BufferedImage capture = new Robot().createScreenCapture(sourceScreenRect);
Mat sourceMat = CvHelper.convertToMat(capture);
Mat templateMat = Imgcodecs.imread(templateImage.getAbsolutePath());
return this.findImage(sourceMat, templateMat, desiredAccuracy);
} catch (Exception ex) {
throw new RuntimeException(String.format(
"An error ocurred while trying to find an image on screen at (%s, %s, %s, %s)",
sourceScreenRect.x,
sourceScreenRect.y,
sourceScreenRect.width,
sourceScreenRect.height), ex);
}
}
示例4: loadResource
import org.opencv.imgcodecs.Imgcodecs; //导入依赖的package包/类
public static Mat loadResource(Context context, int resourceId, int flags) throws IOException
{
InputStream is = context.getResources().openRawResource(resourceId);
ByteArrayOutputStream os = new ByteArrayOutputStream(is.available());
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
is.close();
Mat encoded = new Mat(1, os.size(), CvType.CV_8U);
encoded.put(0, 0, os.toByteArray());
os.close();
Mat decoded = Imgcodecs.imdecode(encoded, flags);
encoded.release();
return decoded;
}
示例5: run
import org.opencv.imgcodecs.Imgcodecs; //导入依赖的package包/类
public void run() {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String base = "C:/Books in Progress/Java for Data Science/Chapter 10/OpenCVExamples/src/resources";
CascadeClassifier faceDetector =
new CascadeClassifier(base + "/lbpcascade_frontalface.xml");
Mat image = Imgcodecs.imread(base + "/images.jpg");
MatOfRect faceVectors = new MatOfRect();
faceDetector.detectMultiScale(image, faceVectors);
out.println(faceVectors.toArray().length + " faces found");
for (Rect rect : faceVectors.toArray()) {
Imgproc.rectangle(image, new Point(rect.x, rect.y),
new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 255, 0));
}
Imgcodecs.imwrite("faceDetection.png", image);
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:21,代码来源:DetectFaceDemo.java
示例6: smoothImage
import org.opencv.imgcodecs.Imgcodecs; //导入依赖的package包/类
public void smoothImage() {
// Smoothing, also called blurring, will make the edges soother.
Mat source = Imgcodecs.imread("cat.jpg");
Mat destination = source.clone();
for (int i = 0; i < 25; i++) {
Mat sourceImage = destination.clone();
Imgproc.blur(sourceImage, destination, new Size(3.0, 3.0));
}
Imgcodecs.imwrite("smoothCat.jpg", destination);
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:11,代码来源:OpenCVNonMavenExamples.java
示例7: sharpenImage
import org.opencv.imgcodecs.Imgcodecs; //导入依赖的package包/类
public void sharpenImage() {
String fileName = "SharpnessExample2.png";
fileName = "smoothCat.jpg";
fileName = "blurredText.jpg";
fileName = "Blurred Text3.jpg";
try {
// Not working that well !!!
Mat source = Imgcodecs.imread(fileName,
// Imgcodecs.CV_LOAD_IMAGE_COLOR);
Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
Mat destination = new Mat(source.rows(), source.cols(), source.type());
Imgproc.GaussianBlur(source, destination, new Size(0, 0), 10);
// The following was used witht he cat
// Core.addWeighted(source, 1.5, destination, -0.75, 0, destination);
// Core.addWeighted(source, 2.5, destination, -1.5, 0, destination);
Core.addWeighted(source, 1.5, destination, -0.75, 0, destination);
Imgcodecs.imwrite("sharpenedCat.jpg", destination);
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例8: CSVRead
import org.opencv.imgcodecs.Imgcodecs; //导入依赖的package包/类
/**
* 读取CSV文件
* @param filePath 文件的绝对路径
* @param matLists
* @param labels
*/
public static void CSVRead(String filePath, List<Mat> matLists, List<Integer> labels) {
try {
File file = new File(filePath);
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
while((line = br.readLine()) != null) {
String [] tmp = line.split(";");
if(!tmp[0].isEmpty() && !tmp[1].isEmpty()) {
matLists.add(Imgcodecs.imread(tmp[0], Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE));
labels.add(Integer.valueOf(tmp[1]));
}
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
示例9: loadImage
import org.opencv.imgcodecs.Imgcodecs; //导入依赖的package包/类
public static void loadImage(String saveRoute, List<Mat> matLists) {
File saveRoot = new File(saveRoute);
if(!saveRoot.exists()) {
LOG.info("图片保存路径--" + saveRoute + "--不存在");
return;
}
if(!saveRoot.isDirectory()) {
LOG.info("图片路径--" + saveRoute+ "--不是一个文件夹");
return;
}
File[] procImages = saveRoot.listFiles(new ImageFileFilter());
for(int i = 0; i < procImages.length; i ++) {
//LOG.info("加载图片:" + procImages[i].getAbsolutePath());
Mat m = Imgcodecs.imread(procImages[i].getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
matLists.add(m);
}
}
示例10: main
import org.opencv.imgcodecs.Imgcodecs; //导入依赖的package包/类
public static void main(String[] args) {
String imgPath = "src/main/resources/dcp_images/flash/cave-flash.bmp";
String guidedImgPath = "src/main/resources/dcp_images/flash/cave-noflash.bmp";
Mat image = Imgcodecs.imread(imgPath, Imgcodecs.CV_LOAD_IMAGE_COLOR);
new ImShow("image").showImage(image);
image.convertTo(image, CvType.CV_32F);
Mat guide = Imgcodecs.imread(guidedImgPath, Imgcodecs.CV_LOAD_IMAGE_COLOR);
guide.convertTo(guide, CvType.CV_32F);
List<Mat> img = new ArrayList<>();
List<Mat> gid = new ArrayList<>();
Core.split(image, img);
Core.split(guide, gid);
int r = 8;
double eps = 0.02 * 0.02;
Mat q_r = Filters.GuidedImageFilter(img.get(0), gid.get(0), r, eps);
Mat q_g = Filters.GuidedImageFilter(img.get(1), gid.get(1), r, eps);
Mat q_b = Filters.GuidedImageFilter(img.get(2), gid.get(2), r, eps);
Mat q = new Mat();
Core.merge(new ArrayList<>(Arrays.asList(q_r, q_g, q_b)), q);
q.convertTo(q, CvType.CV_8UC1);
new ImShow("q").showImage(q);
}
示例11: main
import org.opencv.imgcodecs.Imgcodecs; //导入依赖的package包/类
public static void main(String[] args) {
String imgPath = "src/main/resources/dcp_images/enhancement/tulips.bmp";
Mat image = Imgcodecs.imread(imgPath, Imgcodecs.CV_LOAD_IMAGE_COLOR);
new ImShow("image").showImage(image);
image.convertTo(image, CvType.CV_32F);
List<Mat> img = new ArrayList<>();
Core.split(image, img);
int r = 16;
double eps = 0.01;
Mat q_r = Filters.GuidedImageFilter(img.get(0), img.get(0), r, eps);
Mat q_g = Filters.GuidedImageFilter(img.get(1), img.get(1), r, eps);
Mat q_b = Filters.GuidedImageFilter(img.get(2), img.get(2), r, eps);
Mat q = new Mat();
Core.merge(new ArrayList<>(Arrays.asList(q_r, q_g, q_b)), q);
q.convertTo(q, CvType.CV_8UC1);
new ImShow("q").showImage(q);
}
示例12: processStillImage
import org.opencv.imgcodecs.Imgcodecs; //导入依赖的package包/类
private void processStillImage() {
Mat mat = Imgcodecs.imread("fly.bmp");
for (int c = 0; c < mat.width() / 2; c++) {
for (int r = 0; r < mat.height() / 2; r++) {
double color[] = mat.get(r, c);
color[0] = 255;
mat.put(r, c, color);
//System.out.printf("(%d, %d) = %s\n", r, c, Arrays.toString(color));
}
}
Imgcodecs.imwrite("fly_new.bmp", mat);
Mat gray = new Mat();
Imgproc.cvtColor(mat, gray, Imgproc.COLOR_RGB2GRAY);
Imgcodecs.imwrite("fly_gray.bmp", gray);
}
示例13: FloodFillDemo
import org.opencv.imgcodecs.Imgcodecs; //导入依赖的package包/类
public FloodFillDemo() {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
frame1 = Imgcodecs.imread("frame1.png");
frame2 = Imgcodecs.imread("frame2.png");
grayscaleMat = frame1.clone();
originalMat = frame1.clone();
// apply gaussian filter on both the frames to cancel out noise
Imgproc.blur(frame1, frame1, new Size(5.0, 5.0));
Imgproc.blur(frame2, frame2, new Size(5.0, 5.0));
System.out.println("Height " + frame1.height());
System.out.println("Width " + frame1.width());
calculateDifference(frame1, frame2, grayscaleMat);
Imgcodecs.imwrite("difference.png", grayscaleMat);
// since we use the result from one process in another
// at the end of the last process (here process(2))
// we will have the final output (including all three channels)
// written in output2.png
process(0);
process(1);
process(2);
}
示例14: staticFace
import org.opencv.imgcodecs.Imgcodecs; //导入依赖的package包/类
public static void staticFace(String input, String output) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.out.println("\nRunning FaceDetector");
final CascadeClassifier faceDetector = new CascadeClassifier(StaticFacialRecognition.class
.getResource("../../../../../opencv/sources/data/haarcascades_cuda/haarcascade_frontalface_alt.xml")
.getPath().substring(1));
final Mat image = Imgcodecs.imread(input);
// �������� StaticFacialRecognition.class.getResource(input).getPath().substring(1)
final MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
for (final Rect rect : faceDetections.toArray()) {
Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 255, 0));
}
System.out.println(String.format("Writing %s", output));
Imgcodecs.imwrite(output, image);
// �������� StaticFacialRecognition.class.getResource("").getPath().substring(1) +
// output
}
示例15: enhanceImageBrightness
import org.opencv.imgcodecs.Imgcodecs; //导入依赖的package包/类
public void enhanceImageBrightness() {
double alpha = 1; // Change to 2 for more brightness
double beta = 50;
String fileName = "cat.jpg";
Mat source = Imgcodecs.imread("cat.jpg");
Mat destination = new Mat(source.rows(), source.cols(),
source.type());
source.convertTo(destination, -1, 1, 50);
Imgcodecs.imwrite("brighterCat.jpg", destination);
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:12,代码来源:OpenCVNonMavenExamples.java