Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MATLAB:
- I=imread('P_Falc_thin2.jpg');
- figure;enter image description here
- imshow(I)
- I_comp=imcomplement(I); %takes complement of image
- figure;
- imshow(I_comp)
- %Filter cells from background
- level=graythresh(I_comp);
- BW=im2bw(I_comp,level);
- figure;
- imshow(BW);
- cells = bwareafilt(BW, [20 700000]); %size exclusion by area will only keep images within these parameters
- figure;
- imshow(cells)
- cells2 = bwareaopen(cells,100);
- figure;
- imshow(cells2)
- maskedImage = I_comp;
- maskedImage(~cells) = 0; %set whatever is not within this size black
- mask=I_comp>100;
- [enter image description here][1]
- MATLAB:
- I=imread('P_Falc_thin2.jpg');
- figure;enter image description here
- imshow(I)
- I_comp=imcomplement(I); %takes complement of image
- figure;
- imshow(I_comp)
- %Filter cells from background
- level=graythresh(I_comp);
- BW=im2bw(I_comp,level);
- figure;
- imshow(BW);
- cells = bwareafilt(BW, [20 700000]); %size exclusion by area will only keep images within these parameters
- figure;
- imshow(cells)
- cells2 = bwareaopen(cells,100);
- figure;
- imshow(cells2)
- maskedImage = I_comp;
- maskedImage(~cells) = 0; %set whatever is not within this size black
- mask=I_comp>100;
- Java:
- import java.util.Scanner;
- import org.opencv.core.Core;
- import org.opencv.core.Mat;
- import org.opencv.core.CvType;
- import org.opencv.core.Scalar;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import org.opencv.core.*;
- import org.opencv.imgcodecs.*; // imread, imwrite, etc
- import org.opencv.imgproc.Imgproc;
- public class ProcessImage {
- public static void main(String[] args) {
- System.loadLibrary("opencv_java320");
- //File name info:
- String file = "C:/Users/Nirali Patel/Desktop/Java/thinSmear1";
- String ext = ".jpg";
- String pathToFile = file+ext;
- // Mat is the image matrix
- Mat mat=Imgcodecs.imread(pathToFile, Imgcodecs.CV_LOAD_IMAGE_COLOR);
- LoadImage(file,ext,mat, "_Input");
- // Complement Image
- Mat mat_comp = mat;
- Core.bitwise_not(mat,mat_comp);
- Mat mat_grayscale = mat_comp;
- LoadImage(file,ext,mat_grayscale,"_Complement_Input");
- // GrayScale Image
- Imgproc.cvtColor(mat_comp, mat_grayscale, Imgproc.COLOR_RGB2GRAY,0);
- LoadImage(file,ext,mat_grayscale,"_inverted&grayscale");
- // threshold cells
- Mat mat_thresh = mat_comp;
- Imgproc.threshold(mat_thresh, mat_comp, 0, 255, Imgproc.THRESH_OTSU);
- LoadImage(file,ext,mat_thresh,"thresh");
- //Remove background noise
- Mat mat_morph = mat_thresh;
- Imgproc.blur(mat_grayscale, mat_morph, new Size (20,20));
- LoadImage(file,ext,mat_morph, "morph");
- //Edge Detection
- Mat mat_edges= mat_morph;
- Imgproc.Canny(mat_edges, mat_morph, 5, 75, 3, true);
- LoadImage(file,ext,mat_edges, "Edges");
- //Close Image
- Mat dilateElement = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(30, 10));
- Mat erodeElement = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(7, 1));
- Mat mat_dilate= mat_edges;
- Imgproc.dilate(mat_dilate, mat_edges, dilateElement);
- LoadImage(file,ext, mat_dilate, "Dilate Cells");
- Mat mat_erode=mat_dilate;
- Imgproc.erode(mat_dilate, mat_edges, erodeElement);
- LoadImage(file,ext, mat_erode, "Erode Cells");
- // create temporary image that will hold the mask
- //Mat mask_image( your_image.size(), CV_8U, Scalar(0));
- // draw your contour in mask
- //drawContours(mask_image, contours, ind, Scalar(255), CV_FILLED);
- // copy only non-zero pixels from your image to original image
- //your_image.copyTo(original_image, mask_image);
- }
- public static void LoadImage(String imgStr,
- String ext,Mat m, String name){
- String new_img_name = imgStr+name+ext;
- Imgcodecs.imwrite(new_img_name,m);
- JFrame frame = new JFrame(name);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- // Inserts the image icon
- ImageIcon I = new ImageIcon(new_img_name);
- frame.setSize(I.getIconWidth()+10,I.getIconHeight()+35);
- // Draw the Image data into the BufferedImage
- JLabel label1 = new JLabel(" ", I, JLabel.CENTER);
- frame.getContentPane().add(label1);
- frame.validate();
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement