Переделай код, без String thresholdString = JOptionPane.showInputDialog(this, "Введите…
- User
Переделай код, без String thresholdString = JOptionPane.showInputDialog(this, "Введите пороговое значение: "); int threshold = Integer.parseInt(thresholdString); } private void contrastImage() { if (image != null) { // Получение порогового значения String thresholdString = JOptionPane.showInputDialog(this, "Введите пороговое значение: "); int threshold = Integer.parseInt(thresholdString);
BufferedImage contrastStretchedImage = linearContrastStretch(image); setImageIcon(contrastStretchedImage); saveImage(contrastStretchedImage); // Создаем гистограмму исходного изображения int[] histogram = new int[256]; for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { int pixel = image.getRGB(i, j); int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = pixel & 0xff; // Вычисляем яркость пикселя int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue); if (brightness > threshold){ // Преобразуем яркость пикселя до максимального значения, если она больше порога brightness = 255; } histogram[brightness]++; } } - Assistant
private void contrastImage() { if (image != null) { // Ввод порогового значения int threshold = -1; boolean validInput = false; while (!validInput) { try { String thresholdString = JOptionPane.showInputDialog(this, "Введите пороговое значение: "); threshold = Integer.parseInt(thresholdString); validInput = true; } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "Неверный ввод. Введите целое число."); } catch (NullPointerException e) { return; // пользователь отменил ввод } }
BufferedImage contrastStretchedImage = linearContrastStretch(image); setImageIcon(contrastStretchedImage); saveImage(contrastStretchedImage); // Создаем гистограмму исходного изображения int[] histogram = new int[256]; for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { int pixel = image.getRGB(i, j); int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = pixel & 0xff; // Вычисляем яркость пикселя int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue); if (brightness > threshold) { // Преобразуем яркость пикселя до максимального значения, если она больше порога brightness = 255; } histogram[brightness]++; } } } - User
Сделай без ввода. private void contrastImage() { if (image != null) { // Получение порогового значения String thresholdString = JOptionPane.showInputDialog(this, "Введите пороговое значение: "); int threshold = Integer.parseInt(thresholdString);
BufferedImage contrastStretchedImage = linearContrastStretch(image); setImageIcon(contrastStretchedImage); saveImage(contrastStretchedImage); // Создаем гистограмму исходного изображения int[] histogram = new int[256]; for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { int pixel = image.getRGB(i, j); int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = pixel & 0xff; // Вычисляем яркость пикселя int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue); if (brightness > threshold){ // Преобразуем яркость пикселя до максимального значения, если она больше порога brightness = 255; } histogram[brightness]++; } } - Assistant
private void contrastImage(int threshold) { if (image != null) { BufferedImage contrastStretchedImage = linearContrastStretch(image);
setImageIcon(contrastStretchedImage); saveImage(contrastStretchedImage); // Создаем гистограмму исходного изображения int[] histogram = new int[256]; for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { int pixel = image.getRGB(i, j); int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = pixel & 0xff; // Вычисляем яркость пикселя int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue); if (brightness > threshold) { // Преобразуем яркость пикселя до максимального значения, если она больше порога brightness = 255; } histogram[brightness]++; } } }}
// Добавить вызов метода contrastImage() с нужным значением порога.