001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.demos.sandbox; 031 032 033import javax.swing.JButton; 034import javax.swing.JFrame; 035import javax.swing.JLabel; 036import javax.swing.JTextField; 037 038import java.awt.GridBagLayout; 039 040 041import org.openimaj.image.DisplayUtilities; 042import org.openimaj.image.ImageUtilities; 043import org.openimaj.image.MBFImage; 044import org.openimaj.image.pixel.ConnectedComponent; 045import org.openimaj.image.segmentation.FelzenszwalbHuttenlocherSegmenter; 046import org.openimaj.image.segmentation.SegmentationUtilities; 047import org.openimaj.image.typography.hershey.HersheyFont; 048 049import java.awt.GridBagConstraints; 050import java.awt.event.ActionEvent; 051import java.awt.event.ActionListener; 052import java.io.IOException; 053import java.net.MalformedURLException; 054import java.net.URL; 055import java.util.List; 056 057public class SegmentationTester extends JFrame{ 058 private static final long serialVersionUID = 1L; 059 060 private float K = 5; 061 private float SIGMA = (float) 0.5; 062 private float MIN_PIXELS_FRACTION = (float) 0.01; 063 064 //final FelzenszwalbHuttenlocherSegmenter FSegmenter = new FelzenszwalbHuttenlocherSegmenter(SIGMA, K, 1000); 065 066 067 068 public SegmentationTester() { 069 070 071 this.setTitle("FelzenszwalbHuttenlocherSegmenter"); 072 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 073 074 GridBagLayout gridBagLayout = new GridBagLayout(); 075 GridBagConstraints gbc_panel = new GridBagConstraints(); 076 this.setLayout(gridBagLayout); 077 078 JLabel kLabel = new JLabel("K value"); 079 JLabel thresholdLabel = new JLabel("SIGMA value"); 080 JLabel minSizeLabel = new JLabel("Mininum #pixels/segment"); 081 082 final JTextField kField = new JTextField(""+K); 083 final JTextField thresholdField = new JTextField(""+SIGMA); 084 final JTextField minSizeField = new JTextField(""+MIN_PIXELS_FRACTION); 085 086 gbc_panel.gridx = 0; 087 gbc_panel.gridy = 0; 088 getContentPane().add(kLabel, gbc_panel); 089 090 gbc_panel.gridx = 1; 091 gbc_panel.gridy = 0; 092 getContentPane().add(kField, gbc_panel); 093 094 095 gbc_panel.gridx = 0; 096 gbc_panel.gridy = 1; 097 getContentPane().add(thresholdLabel, gbc_panel); 098 099 gbc_panel.gridx = 1; 100 gbc_panel.gridy = 1; 101 getContentPane().add(thresholdField, gbc_panel); 102 103 104 gbc_panel.gridx = 0; 105 gbc_panel.gridy = 2; 106 getContentPane().add(minSizeLabel, gbc_panel); 107 108 gbc_panel.gridx = 1; 109 gbc_panel.gridy = 2; 110 getContentPane().add(minSizeField, gbc_panel); 111 112 113 JLabel urlLabel = new JLabel("URL"); 114 gbc_panel.gridx = 0; 115 gbc_panel.gridy = 3; 116 getContentPane().add(urlLabel, gbc_panel); 117 118 final JTextField urlField = new JTextField(30); 119 gbc_panel.gridx = 0; 120 gbc_panel.gridy = 4; 121 gbc_panel.gridwidth = 2; 122 getContentPane().add(urlField, gbc_panel); 123 124 JButton segment = new JButton("Segment Image"); 125 segment.addActionListener(new ActionListener(){ 126 @Override 127 public void actionPerformed(ActionEvent e){ 128 129 K = Float.parseFloat(kField.getText().trim()); 130 SIGMA = Float.parseFloat(thresholdField.getText().trim()); 131 MIN_PIXELS_FRACTION = Float.parseFloat(minSizeField.getText().trim()); 132 133 try { 134 MBFImage image = ImageUtilities.readMBF(new URL(urlField.getText().trim())); 135 136 int pixelNumberInImage = image.getRows()*image.getCols(); 137 int mininumPixelsInSegment = Math.round(((float)pixelNumberInImage) * MIN_PIXELS_FRACTION); 138 139 FelzenszwalbHuttenlocherSegmenter<MBFImage> FSegmenter = new FelzenszwalbHuttenlocherSegmenter<MBFImage>(SIGMA, K, mininumPixelsInSegment); 140 141 List < ConnectedComponent > segments = FSegmenter.segment(image); 142 143 MBFImage segImage = SegmentationUtilities.renderSegments(image, segments); 144 for(int i=0; i<segments.size(); i++){ 145 segImage.drawText("Region" + i , segments.get(i).calculateCentroidPixel().x, 146 segments.get(i).calculateCentroidPixel().y, 147 HersheyFont.TIMES_BOLD, 20); 148 } 149 //DisplayUtilities.display(image); 150 DisplayUtilities.display(segImage); 151 System.out.println(segments.size()); 152 153// FSegmenter = null; 154// segments = null; 155// Runtime.getRuntime().gc(); 156 157 158 159 160 161 } catch (MalformedURLException e1) { 162 // TODO Auto-generated catch block 163 e1.printStackTrace(); 164 } catch (IOException e1) { 165 // TODO Auto-generated catch block 166 e1.printStackTrace(); 167 } 168 169 } 170 }); 171 gbc_panel.gridx = 0; 172 gbc_panel.gridy = 5; 173 gbc_panel.gridwidth = 1; 174 getContentPane().add(segment, gbc_panel); 175 176 this.pack(); 177 this.setVisible(true); 178 this.setResizable(false); 179 180 } 181 182 183 /** 184 * @param args 185 */ 186 public static void main(String[] args) { 187 // TODO Auto-generated method stub 188 SegmentationTester tester = new SegmentationTester(); 189 } 190 191}