首页 > 其他分享 >图像增强

图像增强

时间:2023-12-10 23:44:59浏览次数:22  
标签:String void private public import new 图像增强

package com.example;

import okhttp3.*;
import org.json.JSONException;
import org.json.JSONObject;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class tuxiang extends JFrame {
private static final long serialVersionUID = 1L;
public static final String API_KEY = "TggSZoWYfVLpaGbezMzf2opT";
public static final String SECRET_KEY = "eF9UfmITtR0EUDFkDMyHyCcbKGHe9j8Y";
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
private JLabel imageLabel;
private JButton chooseButton;
private JButton processButton;

private String selectedImagePath;

public tuxiang() {
setTitle("Image Processing App");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

initializeComponents();
setLayout(new BorderLayout());

add(imageLabel, BorderLayout.CENTER);
add(chooseButton, BorderLayout.WEST);
add(processButton, BorderLayout.EAST);
}

private void initializeComponents() {
imageLabel = new JLabel();
chooseButton = new JButton("Choose Image");
processButton = new JButton("Process Image");

chooseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chooseImage();
}
});
processButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
processImage();
}
});
}
//用来选择文件位置
private void chooseImage() {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(this);

if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
selectedImagePath = selectedFile.getAbsolutePath();
displayImage(selectedImagePath);
}
}

private void displayImage(String imagePath) {
ImageIcon icon = new ImageIcon(imagePath);
Image image = icon.getImage().getScaledInstance(300, 300, Image.SCALE_SMOOTH);
imageLabel.setIcon(new ImageIcon(image));
}

private void processImage() {
if (selectedImagePath != null && !selectedImagePath.isEmpty()) {
try {
String base64Image = getFileContentAsBase64(selectedImagePath, true);
String processedImage = processImageWithBaiduAPI(base64Image);
displayImage(processedImage);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Image processing failed: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(this, "Please choose an image first.", "Error", JOptionPane.ERROR_MESSAGE);
}
}

private String getFileContentAsBase64(String selectedImagePath, boolean b) {
return selectedImagePath;
}

private String processImageWithBaiduAPI(String base64Image) throws Exception {
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "image=" + base64Image);
Request request = new Request.Builder()
.url("https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime?access_token=" + getAccessToken())
.method("POST", body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("Accept", "application/json")
.build();
Response response = HTTP_CLIENT.newCall(request).execute();

return new JSONObject(response.body().string()).getString("processed_image");
// Assuming there's a "processed_image" field in the response
}
static String getAccessToken() throws IOException {
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
+ "&client_secret=" + SECRET_KEY);
Request request = new Request.Builder()
.url("https://aip.baidubce.com/oauth/2.0/token")
.method("POST", body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
Response response = HTTP_CLIENT.newCall(request).execute();
try {
return new JSONObject(response.body().string()).getString("access_token");
} catch (JSONException e) {
throw new RuntimeException(e);
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new tuxiang().setVisible(true);
}
});
}
}

标签:String,void,private,public,import,new,图像增强
From: https://www.cnblogs.com/mine-my/p/17893496.html

相关文章

  • 使用百度完成gui的图像处理(需要下载百度的javasdk文档,主要工具带代码在sdk之中,以下代
    packageGui;importcom.baidu.aip.imageprocess.AipImageProcess;importorg.json.JSONObject;importjavax.imageio.ImageIO;importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.i......
  • 机器视觉中的图像增强与对比度调整技术
     在机器视觉中,图像增强与对比度调整技术是常用的方法,旨在改善图像的质量和视觉效果,或将图像转换成更适合人眼观察或机器分析识别的形式。图像增强技术可以针对给定图像的应用场合,有目的地强调图像的整体或局部特性,将原来不清晰的图像变得清晰或强调某些感兴趣的特征。例如,可以扩......
  • G-channel 实现低光图像增强
    G-channel之前研究低光图像增强时,看到一篇博客,里面介绍了一种方法,没有说明出处,也没有说明方法的名字,这里暂时叫做G-channel算法。博客地址:低照度图像增强(附步骤及源码)_低照度图像增强算法_grafx的博客-CSDN博客算法过程博客里面简单说明了算法的执行过程,这里重复如下:将绿......
  • ISP之红外图像增强处理算法
    1、红外图像1.1红外图像特点红外图像一般具有以下特点(一般中长波特点更明显):1)红外图像表征景物的温度分布,反映目标及背景向外辐射能量的差异,是灰度图像,像素分辨率低;2)红外探测气球收到加工工艺影响,靶面分辨率较低,1280x1024分辨率属于高分辨率,640x512的规格较多;3)红外波段会受到......
  • FPGA图像增强,基于FPGA的图像去雾处理,算法为暗通道先验,并在matlab上实现了算法的仿真,使
    FPGA图像增强,基于FPGA的图像去雾处理,算法为暗通道先验,并在matlab上实现了算法的仿真,使用的软件为quartus13.0。注意在FPGA上实现时,在浓雾区域和天空区域的处理效果不算太好。ID:48300647242454158......
  • 图像增强算法受环境影响几种校正方式
    图像增强环境影响几种校正方式由于受到环境,光线、噪音、不同设备拍摄的清晰度和对比度等也会影响到图像最终的采集效果,不能够直接采取图像中的重点部分。以下几种校正方式可以单独应用或者结合使用,以根据图像的特征和需求来提高图像的质量和视觉效果。根据不同的应用场景和目标,选择......
  • 【计算机视觉1】----- 图像增强算法(对比度增强、直方图均衡化)
    直方图均衡化直方图修正(HistogramEqualization)是一种常见的图像增强技术,它通过重新分布图像像素的灰度值来增强图像的对比度和亮度。直方图修正的基本思想是将图像的灰度值范围映射到一个更广泛的范围,从而使图像的灰度级分布更加均匀。注意,在运行代码之前,请确保已安装并配置了Ope......
  • Python从零到壹丨图像增强的顶帽运算和底帽运算
    摘要:这篇文章详细介绍了顶帽运算和底帽运算,它们将为后续的图像分割和图像识别提供有效支撑。本文分享自华为云社区《[Python从零到壹]四十九.图像增强及运算篇之顶帽运算和底帽运算》,作者:eastmount。数学形态学(MathematicalMorphology)是一种应用于图像处理和模式识别领域的新......
  • MATLAB图像增强代码 代码些许复杂,由本人一个朋友编写 是
    MATLAB图像增强代码代码些许复杂,由本人一个朋友编写是机器视觉和图像增强领域的应用,有gui界面,可以载入原图和参照强化的图像,读取参照图像的RGB或者HSV分量,并强化原图像,运行,corrction.m.结果如下图ID:925620010658083......
  • 图像增强
    两类图像增强方法空域:直接对像素操作频域:傅里叶变换,小波变换空域图像灰度级分布特点对比度越高,灰度级在空间内分布更加均匀。 直方图均衡化计算步骤原始图像直方图->累积概率分布直方图->均值化后的直方图核心:将累积概率分布的值以为总灰度级个数对其进行近似划......