• R/O
  • SSH
  • HTTPS

コミット

タグ
未設定

よく使われているワード(クリックで追加)

javaandroidc++linuxc#objective-ccocoa誰得qtrubybathyscaphegamephpguiwindowsc翻訳pythonomegattwitterframeworkbtronarduinovb.net計画中(planning stage)directxpreviewertestゲームエンジンdom

コミットメタ情報

リビジョン2 (tree)
日時2016-10-01 06:55:35
作者kmorimatsu

ログメッセージ

KM_DNA_Length_Measurement ver 1.001

変更サマリ

差分

--- branches/kmDNAlengthMeasurement/KM_DNA_Length_Measurement.java (nonexistent)
+++ branches/kmDNAlengthMeasurement/KM_DNA_Length_Measurement.java (revision 2)
@@ -0,0 +1,357 @@
1+/*******************************************
2+ * Image J DNA length measurement plugin *
3+ * Constructed by Katsumi and Ichiro *
4+ * KM_DNA_Length Measurement ver 1.1 *
5+ *******************************************/
6+import ij.*;
7+import ij.process.*;
8+import ij.gui.*;
9+import ij.plugin.*;
10+import ij.measure.*;
11+
12+import org.opencv.core.*;
13+import org.opencv.imgcodecs.*;
14+import org.opencv.imgproc.*;
15+
16+import java.awt.image.*;
17+import java.util.*;
18+import java.lang.*;
19+
20+public class KM_DNA_Length_Measurement implements PlugIn
21+{
22+ // ij.process.ImageProcessor
23+ ImageProcessor src;
24+ // ij.ImagePlus
25+ ImagePlus img_plus;
26+ // java.awt.image.BufferedImage
27+ BufferedImage bi;
28+ int h;
29+ int w;
30+ // ij.ImageStack
31+ ImageStack stack;
32+ int stack_size;
33+ byte[] data;
34+ // ij.measure.ResultsTable
35+ ResultsTable rt;
36+ // ij.gui.Roi
37+ Roi imageRoi;
38+ double imageRoiLeft,imageRoiRight,imageRoiUp,imageRoiBottom;
39+ double imageRoiLineWidth, imageRoiLeftX, imageRoiLeftY, imageRoiAngle, imageRoiLength;
40+ double imageRoiRightX, imageRoiRightY;
41+
42+ public void run(String arg)
43+ {
44+ /* Fetch the information of image, size and # of frames
45+ img_plus is an object containing image stack to be analyzed. */
46+ // ij.WindowManager
47+ img_plus = WindowManager.getCurrentImage();
48+ if (img_plus==null) {
49+ IJ.showMessage("Please select an image.");
50+ return;
51+ }
52+
53+ /* Fetch information of ROI */
54+ // ij.gui.Roi
55+ imageRoi=img_plus.getRoi();
56+ if (imageRoi != null) {
57+ switch(imageRoi.getType()) {
58+ case Roi.RECTANGLE:
59+ imageRoiLeft=imageRoi.getXBase();
60+ imageRoiRight=imageRoi.getXBase()+imageRoi.getFloatWidth();
61+ imageRoiUp=imageRoi.getYBase();
62+ imageRoiBottom=imageRoi.getYBase()+imageRoi.getFloatHeight();
63+ imageRoiLineWidth=0;
64+ break;
65+ case Roi.LINE:
66+ imageRoiLineWidth=imageRoi.getStrokeWidth();
67+ imageRoiLeftX=imageRoi.getXBase();
68+ imageRoiLeftY=imageRoi.getYBase();
69+ imageRoiAngle=Math.PI*imageRoi.getAngle()/180;
70+ imageRoiLength=imageRoi.getLength();
71+ imageRoiRightX=imageRoiLeftX+imageRoiLength*Math.cos(imageRoiAngle);
72+ imageRoiRightY=imageRoiLeftY-imageRoiLength*Math.sin(imageRoiAngle);
73+ if (imageRoiLineWidth<1) imageRoiLineWidth=1;
74+ debug("LINE Roi, width: "+String.valueOf(imageRoiLineWidth));
75+ debug(String.valueOf(imageRoiLeftX));
76+ debug(String.valueOf(imageRoiLeftY));
77+ debug(String.valueOf(imageRoiAngle));
78+ debug(String.valueOf(imageRoiLength));
79+ debug(String.valueOf(imageRoiRightX));
80+ debug(String.valueOf(imageRoiRightY));
81+ return;
82+ default:
83+ debug("ROI must be rectangle!");
84+ return;
85+ }
86+ }
87+
88+ stack_size = img_plus.getStackSize();
89+ h = img_plus.getProcessor().getHeight();
90+ w = img_plus.getProcessor().getWidth();
91+
92+ /* New stack object containing the results */
93+ stack = new ImageStack(w, h);
94+
95+ rt = new ResultsTable();
96+ rt.reset();
97+
98+ /* Initialize Open CV */
99+ try {
100+ System.loadLibrary("opencv_java300");
101+ } catch (UnsatisfiedLinkError e) {
102+ IJ.showMessage("Please restart ImageJ.");
103+ return;
104+ }
105+
106+ /* For each stacks in image file */
107+ for (int stack_num = 0; stack_num < stack_size; stack_num++)
108+ {
109+ // Show the current slice of image to be analyzed
110+ img_plus.setSlice(stack_num + 1);
111+
112+ // Fetch the current image processor
113+ src = img_plus.getProcessor();
114+ //gb.blurGaussian(src, 1.0, 1.0, 0.01);
115+ //IJ.runMacro("Auto Threshold...", "method=Minimum white stack");
116+
117+ /* start OpenCV ******************************************************/
118+
119+ Mat m = byte2Mat(src);
120+
121+ /* Array of MatOfPoint is used to get the data from Imgproc.FindContours method.*/
122+ // java.util.ArrayList, org.opencv.core.MatOfPoint
123+ ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
124+ /* Get the contours array by using OpenCV */
125+ // org.opencv.imgproc.Imgproc
126+ Imgproc.findContours(m, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE);
127+
128+ /* If ROI exists, pick up a contour that overlap to this ROI */
129+ int roi_hi_score=0;
130+ if (imageRoi!=null) {
131+ // Check the scores to fit to ROI, and store the maximum case as contour[0]
132+ for (int i=0;i<contours.size(); i++) {
133+ int j=roiScore(contours.get(i));
134+ if (roi_hi_score<=j) {
135+ roi_hi_score=j;
136+ contours.set(0,contours.get(i));
137+ }
138+ }
139+ // contours array-list will have only one contour
140+ while (1<contours.size()) {
141+ contours.remove(contours.size()-1);
142+ }
143+ }
144+
145+ // org.opencv.core.Point
146+ ArrayList<Point> right_pts = new ArrayList<Point>();
147+ ArrayList<Point> left_pts = new ArrayList<Point>();
148+ ArrayList<Double> distances = new ArrayList<Double>();
149+
150+ /* For each contuors found in the image */
151+ for (int num_contour = 0; num_contour < contours.size(); num_contour++)
152+ {
153+ MatOfPoint shape = contours.get(num_contour);
154+
155+ List<Point> shape_pts_list = Arrays.asList(shape.toArray());
156+ // java.util.Collections
157+ Collections.sort(shape_pts_list, ComparatorX());
158+
159+
160+ Point right_pt = new Point(getMaxX(shape_pts_list), getMeanYatMaxX(shape_pts_list));
161+ Point left_pt = new Point(getMinX(shape_pts_list), getMeanYatMinX(shape_pts_list));
162+ // java.lang.Math
163+ Double d = Math.sqrt( Math.pow(right_pt.x - left_pt.x, 2.0) + Math.pow(right_pt.y - left_pt.y, 2.0) );
164+
165+ right_pts.add(right_pt);
166+ left_pts.add(left_pt);
167+ distances.add(d);
168+
169+ Imgproc.drawContours(m, contours, num_contour, new Scalar(255, 255, 255), 1);
170+ Imgproc.line(m, right_pt, left_pt, new Scalar(255, 255, 255), 1);
171+ if (imageRoi==null) {
172+ Imgproc.putText(m, Integer.toString(num_contour), left_pt, Core.FONT_HERSHEY_PLAIN, 1.5, new Scalar(255, 255, 255), 1);
173+ }
174+ }
175+ data = Mat2byte(m);
176+ /* end OpenCV ********************************************************/
177+
178+ stack.addSlice("", data);
179+ writeResults(stack_num, left_pts, right_pts, distances);
180+ }
181+ rt.show("DNA Length");
182+ (new ImagePlus("Pattern Recognition", stack)).show();
183+ }
184+
185+ /*****************************************************************************/
186+ /* Convert ImageJ ImageProcessor object to Mat of OpenCV */
187+ /*****************************************************************************/
188+ private Mat byte2Mat(ImageProcessor src){
189+ /* Construct Mat object, using unsigned char (8 bit) */
190+ // org.opencv.core.Mat
191+ Mat m = new Mat(h, w, CvType.CV_8UC1);
192+
193+ /* Fetch the pixels data from ImageProcessor and conver to Mat */
194+ m.put(0, 0, (byte[])src.getPixels()); // byte[] to Mat
195+
196+ return m;
197+ }
198+ /*****************************************************************************/
199+ /* Convert Mat to byte[] */
200+ /*****************************************************************************/
201+ private byte[] Mat2byte(Mat m) {
202+ byte[] data = new byte[h * w];
203+ m.get(0, 0, data); // Mat to byte[]
204+ return data;
205+ }
206+ /*****************************************************************************/
207+ /* */
208+ /*****************************************************************************/
209+ private void writeResults(int row, ArrayList<Point> left_pts, ArrayList<Point> right_pts, ArrayList<Double> distances)
210+ {
211+ rt.incrementCounter();
212+ for(int obj_num = 0; obj_num < left_pts.size(); obj_num++)
213+ {
214+ rt.setValue( "object_" + Integer.toString(obj_num) + "_x1", row, left_pts.get(obj_num).x );
215+ rt.setValue( "object_" + Integer.toString(obj_num) + "_y1", row, left_pts.get(obj_num).y );
216+ rt.setValue( "object_" + Integer.toString(obj_num) + "_x2", row, right_pts.get(obj_num).x );
217+ rt.setValue( "object_" + Integer.toString(obj_num) + "_y2", row, right_pts.get(obj_num).y );
218+
219+ rt.setValue( "object_" + Integer.toString(obj_num) + "_D", row, distances.get(obj_num) );
220+ }
221+ }
222+
223+ private int roiScore(MatOfPoint contour){
224+ int score=0;
225+ // Determine rectangle containing the contour
226+ List<Point> list_contour = Arrays.asList(contour.toArray());
227+ Collections.sort(list_contour, ComparatorX());
228+ double left=getMinX(list_contour);
229+ double right=getMaxX(list_contour);
230+ Collections.sort(list_contour, ComparatorX());
231+ double up=getMinY(list_contour);
232+ double bottom=getMaxY(list_contour);
233+ // If either of verticles in each is in the rectangle of other, increment score.
234+ if (left<=imageRoiLeft && imageRoiLeft<=right && up<=imageRoiUp && imageRoiUp <= bottom) score++;
235+ if (left<=imageRoiRight && imageRoiRight<=right && up<=imageRoiUp && imageRoiUp <= bottom) score++;
236+ if (left<=imageRoiLeft && imageRoiLeft<=right && up<=imageRoiUp && imageRoiBottom <= bottom) score++;
237+ if (left<=imageRoiRight && imageRoiRight<=right && up<=imageRoiUp && imageRoiBottom <= bottom) score++;
238+ if (imageRoiLeft<=left && left<=imageRoiRight && imageRoiUp<=up && up<=imageRoiBottom) score++;
239+ if (imageRoiLeft<=right && right<=imageRoiRight && imageRoiUp<=up && up<=imageRoiBottom) score++;
240+ if (imageRoiLeft<=left && left<=imageRoiRight && imageRoiUp<=bottom && bottom<=imageRoiBottom) score++;
241+ if (imageRoiLeft<=right && right<=imageRoiRight && imageRoiUp<=bottom && bottom<=imageRoiBottom) score++;
242+ return score;
243+ }
244+ /*****************************************************************************/
245+ /* comparator for sorting in ascending order of X */
246+ /*****************************************************************************/
247+ private Comparator<Point> ComparatorX(){
248+ return new Comparator<Point>() {
249+ public int compare(Point a, Point b) { return (int)( a.x - b.x ); }
250+ };
251+ }
252+ /*****************************************************************************/
253+ /* comparator for sorting in ascending order of Y */
254+ /*****************************************************************************/
255+ private Comparator<Point> ComparatorY(){
256+ return new Comparator<Point>() {
257+ public int compare(Point a, Point b) { return (int)( a.y - b.y ); }
258+ };
259+ }
260+
261+ /*****************************************************************************/
262+ /* return max X/Y */
263+ /*****************************************************************************/
264+ private double getMaxX(List<Point> sorted_pts_list)
265+ {
266+ return sorted_pts_list.get(sorted_pts_list.size() - 1).x;
267+ }
268+ private double getMaxY(List<Point> sorted_pts_list)
269+ {
270+ return sorted_pts_list.get(sorted_pts_list.size() - 1).y;
271+ }
272+
273+
274+ /*****************************************************************************/
275+ /* return min X/Y */
276+ /*****************************************************************************/
277+ private double getMinX(List<Point> sorted_pts_list)
278+ {
279+ return sorted_pts_list.get(0).x;
280+ }
281+ private double getMinY(List<Point> sorted_pts_list)
282+ {
283+ return sorted_pts_list.get(0).y;
284+ }
285+
286+
287+ /*****************************************************************************/
288+ /* return mean Y at max X */
289+ /*****************************************************************************/
290+ private double getMeanYatMaxX(List<Point> sorted_pts_list)
291+ {
292+ double max_x = sorted_pts_list.get(sorted_pts_list.size() - 1).x;
293+ double mean_y = 0.0;
294+ int c = 0;
295+
296+ for (int i = 0; i < sorted_pts_list.size(); i++)
297+ {
298+ if (sorted_pts_list.get(i).x == max_x)
299+ {
300+ mean_y += sorted_pts_list.get(i).y;
301+ c++;
302+ }
303+ }
304+
305+ return (double)mean_y/c;
306+ }
307+
308+
309+ /*****************************************************************************/
310+ /* return mean Y at min X */
311+ /*****************************************************************************/
312+ private double getMeanYatMinX(List<Point> sorted_pts_list)
313+ {
314+ double min_x = sorted_pts_list.get(0).x;
315+ double mean_y = 0.0;
316+ int c = 0;
317+
318+ for (int i = 0; i < sorted_pts_list.size(); i++)
319+ {
320+ if (sorted_pts_list.get(i).x == min_x)
321+ {
322+ mean_y += sorted_pts_list.get(i).y;
323+ c++;
324+ }
325+ }
326+
327+ return (double)mean_y/c;
328+ }
329+
330+ /* Debug window */
331+ ij.text.TextWindow debugWindow;
332+ private void debug(String text){
333+ if(debugWindow == null) {
334+ debugWindow = new ij.text.TextWindow("debug","",300,300);
335+ }
336+ debugWindow.append(text);
337+ }
338+ private void debug(int val){
339+ debug(String.valueOf(val));
340+ }
341+ private void debug(short val){
342+ debug(String.valueOf(val));
343+ }
344+ private void debug(byte val){
345+ debug(String.valueOf(val));
346+ }
347+ private void debug(long val){
348+ debug(String.valueOf(val));
349+ }
350+ private void debug(float val){
351+ debug(String.valueOf(val));
352+ }
353+ private void debug(double val){
354+ debug(String.valueOf(val));
355+ }
356+}
357+