Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. MATLAB:
  2. I=imread('P_Falc_thin2.jpg');
  3. figure;enter image description here
  4. imshow(I)
  5. I_comp=imcomplement(I); %takes complement of image
  6. figure;
  7. imshow(I_comp)
  8.  
  9.  
  10. %Filter cells from background
  11. level=graythresh(I_comp);
  12. BW=im2bw(I_comp,level);
  13. figure;
  14. imshow(BW);
  15.  
  16.  
  17.  
  18. cells = bwareafilt(BW, [20 700000]); %size exclusion by area will only keep images within these parameters
  19. figure;
  20. imshow(cells)
  21. cells2 = bwareaopen(cells,100);
  22. figure;
  23. imshow(cells2)
  24. maskedImage = I_comp;
  25. maskedImage(~cells) = 0; %set whatever is not within this size black
  26. mask=I_comp>100;
  27. [enter image description here][1]
  28.  
  29. MATLAB:
  30. I=imread('P_Falc_thin2.jpg');
  31. figure;enter image description here
  32. imshow(I)
  33. I_comp=imcomplement(I); %takes complement of image
  34. figure;
  35. imshow(I_comp)
  36.  
  37.  
  38. %Filter cells from background
  39. level=graythresh(I_comp);
  40. BW=im2bw(I_comp,level);
  41. figure;
  42. imshow(BW);
  43.  
  44.  
  45.  
  46. cells = bwareafilt(BW, [20 700000]); %size exclusion by area will only keep images within these parameters
  47. figure;
  48. imshow(cells)
  49. cells2 = bwareaopen(cells,100);
  50. figure;
  51. imshow(cells2)
  52. maskedImage = I_comp;
  53. maskedImage(~cells) = 0; %set whatever is not within this size black
  54. mask=I_comp>100;
  55.  
  56.  
  57.  
  58. Java:
  59.  
  60.  
  61. import java.util.Scanner;
  62. import org.opencv.core.Core;
  63. import org.opencv.core.Mat;
  64.  
  65. import org.opencv.core.CvType;
  66. import org.opencv.core.Scalar;
  67. import javax.swing.ImageIcon;
  68. import javax.swing.JFrame;
  69. import javax.swing.JLabel;
  70.  
  71.  
  72. import org.opencv.core.*;
  73. import org.opencv.imgcodecs.*; // imread, imwrite, etc
  74. import org.opencv.imgproc.Imgproc;
  75.  
  76.  
  77. public class ProcessImage {
  78.  
  79. public static void main(String[] args) {
  80. System.loadLibrary("opencv_java320");
  81.  
  82. //File name info:
  83. String file = "C:/Users/Nirali Patel/Desktop/Java/thinSmear1";
  84. String ext = ".jpg";
  85. String pathToFile = file+ext;
  86.  
  87. // Mat is the image matrix
  88. Mat mat=Imgcodecs.imread(pathToFile, Imgcodecs.CV_LOAD_IMAGE_COLOR);
  89. LoadImage(file,ext,mat, "_Input");
  90.  
  91. // Complement Image
  92. Mat mat_comp = mat;
  93. Core.bitwise_not(mat,mat_comp);
  94. Mat mat_grayscale = mat_comp;
  95. LoadImage(file,ext,mat_grayscale,"_Complement_Input");
  96.  
  97. // GrayScale Image
  98. Imgproc.cvtColor(mat_comp, mat_grayscale, Imgproc.COLOR_RGB2GRAY,0);
  99. LoadImage(file,ext,mat_grayscale,"_inverted&grayscale");
  100.  
  101. // threshold cells
  102. Mat mat_thresh = mat_comp;
  103. Imgproc.threshold(mat_thresh, mat_comp, 0, 255, Imgproc.THRESH_OTSU);
  104. LoadImage(file,ext,mat_thresh,"thresh");
  105.  
  106.  
  107. //Remove background noise
  108. Mat mat_morph = mat_thresh;
  109. Imgproc.blur(mat_grayscale, mat_morph, new Size (20,20));
  110. LoadImage(file,ext,mat_morph, "morph");
  111.  
  112. //Edge Detection
  113. Mat mat_edges= mat_morph;
  114. Imgproc.Canny(mat_edges, mat_morph, 5, 75, 3, true);
  115. LoadImage(file,ext,mat_edges, "Edges");
  116.  
  117. //Close Image
  118. Mat dilateElement = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(30, 10));
  119. Mat erodeElement = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(7, 1));
  120.  
  121. Mat mat_dilate= mat_edges;
  122. Imgproc.dilate(mat_dilate, mat_edges, dilateElement);
  123. LoadImage(file,ext, mat_dilate, "Dilate Cells");
  124.  
  125. Mat mat_erode=mat_dilate;
  126. Imgproc.erode(mat_dilate, mat_edges, erodeElement);
  127. LoadImage(file,ext, mat_erode, "Erode Cells");
  128. // create temporary image that will hold the mask
  129. //Mat mask_image( your_image.size(), CV_8U, Scalar(0));
  130. // draw your contour in mask
  131. //drawContours(mask_image, contours, ind, Scalar(255), CV_FILLED);
  132. // copy only non-zero pixels from your image to original image
  133. //your_image.copyTo(original_image, mask_image);
  134.  
  135.  
  136.  
  137. }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. public static void LoadImage(String imgStr,
  147. String ext,Mat m, String name){
  148. String new_img_name = imgStr+name+ext;
  149. Imgcodecs.imwrite(new_img_name,m);
  150. JFrame frame = new JFrame(name);
  151. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  152.  
  153. frame.setLocationRelativeTo(null);
  154.  
  155. // Inserts the image icon
  156. ImageIcon I = new ImageIcon(new_img_name);
  157.  
  158. frame.setSize(I.getIconWidth()+10,I.getIconHeight()+35);
  159. // Draw the Image data into the BufferedImage
  160. JLabel label1 = new JLabel(" ", I, JLabel.CENTER);
  161. frame.getContentPane().add(label1);
  162.  
  163. frame.validate();
  164. frame.setVisible(true);
  165.  
  166. }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement