I have the following code, where I read images from directory and use ImageJ Auto Threshold plugin to segment my images.
dir = getDirectory("path");
list = getFileList(dir);
for (i=0; i<list.length; i++)
{
if (endsWith(list[i], ".tif"))
{
open(dir + list[i]);
run("8-bit");
run("Gaussian Blur...", "sigma=2");
setAutoThreshold("Otsu dark");
run("Convert to Mask");
saveAs("TIFF", dir+list[i]);
close();
}
}
I would like to get the threshold value using "Otsu dark" method, and modify that value (e.g. scale it by a factor) and apply it to my images for segmentation.
In an ImageJ macro, use the getThreshold(lower,upper) and setThreshold(lower,upper) methods (here's the documentation).
Your code would look like this then:
dir = getDirectory("path");
list = getFileList(dir);
factor = 1.5;
for (i=0; i<list.length; i++)
{
if (endsWith(list[i], ".tif"))
{
open(dir + list[i]);
run("8-bit");
run("Gaussian Blur...", "sigma=2");
setAutoThreshold("Otsu dark");
getThreshold(lower,upper);
setThreshold(lower,upper*factor);
run("Convert to Mask");
saveAs("TIFF", dir+list[i]);
close();
}
}
If you plan to do more complicated things, consider using another scripting language like the ones provided by Fiji.
Related
I am trying to analyze confocal microscopy image for cFos analysis.
From maxstacked image, I took ROI of DAPI and enlarged the ROI to detect the degree of expression of transgene marker.
So I can find whether expression of transgene Piezo1 is related to increase of cFof level.
Here's the problem, when I took ROI twice by processing DAPI image differently worked well, but this has a consideration that the sequence of both ROI groups might be not same.
So I tried to enlarge the ROI from DAPI image, but then I am having trouble in loop analysis.
The macro runs for ever, but seems like to be saving same file names again and again, even with skipping some of the files in array.
Thank you for reading.
//The one is in trouble
dir1 = getDirectory("Choose Source Directory ");
dir2 = getDirectory("Choose Destination Directory ");
list = getFileList(dir1);
Array.sort(list);
run("Clear Results");
setBatchMode(true);
for (i=0; i<list.length; i++) {
if (endsWith(list[i],"tif")) {
showProgress(i+1, list.length);
open(dir1+list[i]);
title = getTitle();
saveFilename = replace(list[i], "_Maxstacked.tif","");
selectWindow(title);
run("Duplicate...", "title=DAPI duplicate channels=4");;
selectWindow("DAPI");
run("Subtract Background...", "rolling=80");
run("Gaussian Blur...", "sigma=1");
run("8-bit");
run("Auto Threshold", "method=Li white");
run("Convert to Mask");
run("Minimum...", "radius=8");
run("Maximum...", "radius=9");
run("Watershed");
run("Analyze Particles...", "size=20-1000 display exclude clear include overlay add");
roiN = roiManager("count");
if (roiN > 0) {
roiManager("Save", dir2+saveFilename+"_DAPI+_RoiSet.zip");
for(i=0; i<roiN; i++) {
roiManager("Select", i);
run("Enlarge...", "enlarge=2.7");
roiManager("Update");
}
roiManager("Save", dir2+saveFilename+"_DAPI+_for_Piezo1_RoiSet.zip");
roiManager("Delete");
roiManager("Delete");
}
close("*");
}
}
//The one worked
dir1 = getDirectory("Choose Source Directory ");
dir2 = getDirectory("Choose Destination Directory ");
list = getFileList(dir1);
Array.sort(list);
run("Clear Results");
setBatchMode(true);
for (i=0; i<list.length; i++) {
if (endsWith(list[i],"tif")) {
showProgress(i+1, list.length);
open(dir1+list[i]);
title = getTitle();
saveFilename = replace(list[i], "_Maxstacked.tif","");
selectWindow(title);
run("Duplicate...", "title=DAPI duplicate channels=4");
selectWindow(title);
run("Duplicate...", "title=DAPI_for_Piezo1 duplicate channels=4");
selectWindow("DAPI");
run("Subtract Background...", "rolling=80");
run("Gaussian Blur...", "sigma=1");
run("8-bit");
run("Auto Threshold", "method=Li white");
run("Convert to Mask");
run("Minimum...", "radius=8");
run("Maximum...", "radius=9");
run("Watershed");
run("Analyze Particles...", "size=20-1000 display exclude clear include overlay add");
roiN = roiManager("count");
if (roiN > 0) {
roiManager("Save", dir2+saveFilename+"_DAPI+_RoiSet.zip");
roiManager("Delete");
}
selectWindow("DAPI");
run("Subtract Background...", "rolling=80");
run("Gaussian Blur...", "sigma=1");
run("8-bit");
run("Auto Threshold", "method=Li white");
run("Convert to Mask");
run("Minimum...", "radius=8");
run("Maximum...", "radius=16");
run("Watershed");
run("Analyze Particles...", "size=20-1000 display exclude clear include overlay add");
roiN = roiManager("count");
if (roiN > 0) {
roiManager("Save", dir2+saveFilename+"_DAPI+_for_Piezo1_RoiSet.zip");
roiManager("Delete");
}
close("*");
}
}
I think you created an endless loop, because you use i both times. So in the second for loop the i is set back to 0. Just try changing it in the second one (you can choose whatever letter or word).
...
for(n=0; n<roiN; n++) {
roiManager("Select", n);
run("Enlarge...", "enlarge=2.7");
roiManager("Update");
}
...
after hours of trying and searching related topics, I don't get further. I am trying to create a small ImageJ script that splits channels of a picture, saves on .tif version with enhanced contrast (this part works) AND creates a histogram and saves it values as .csv (this part does not work, it doesn't save anything).
I have no idea why it doesn't work so here is my whole code.
input = getDirectory("Input directory");
output = getDirectory("Output directory");
Dialog.create("File type");
Dialog.addString("File suffix: ", ".tif", 5);
Dialog.show();
suffix = Dialog.getString();
processFolder(input);
function processFolder(input) {
list = getFileList(input);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(list[i]))
processFolder("" + input + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}
function processFile(input, output, filename) {
print("Processing: " + input + filename);
open(input + filename);
run("Split Channels");
selectWindow(filename+" (blue)");
close();
selectWindow(filename+" (green)");
close();
selectWindow(filename+" (red)");
run("Histogram");
getHistogram(values, counts, 256);
saveAs("Results", output + "Histogram of " + filename + ".csv");
close();
saveAs("TIFF", output+ "processed_" + filename);
selectWindow("processed_" + filename);
close();
}
I appreciate every input. Cheers!
not sure you can save it as a csv file using the IJ macro language. However here is some code to do the same thing but save it as a txt file (this example only takes the value from 255)
getStatistics(mean, min, max, std, histogram);
white=histogram[255];
// list2 is the list of images in a for loop - adds image name to results
print(white,",", list2[j]+" smi31");
selectWindow("Log");
saveAs("txt", file1+"Result");
Hope this is helpful :-)
I have created a macro that generates results both in the log and results pages. I need a macro that saves both the results and log files into one big excel file and is organized by image name.
input=getDirectory("Choose Source Directory ");
list = getFileList(input);
for (i = 0; i < list.length; i++)
rootangle(input, list[i]);
function angle (input,filename){
open (input + filename);
setTool("angle");
for (i = 0; i < 5; i++){
waitForUser("Select Angle Points");
run("Set Measurements...", " display redirect=None decimal=3");
//run("Measure");
}
setTool("multipoint");
waitForUser("Count");
run("Set Measurements...", " display redirect=None decimal=3");
run("Measure");
}
setTool("line");
waitForUser("Measure");
run("Set Measurements...", " display redirect=None decimal=3");
run("Measure");
}
setTool("freehand");
waitForUser("Distance");
run("Set Measurements...", " display redirect=None decimal=3");
run("Measure");
}
setTool("polyline");
waitForUser("Draw");
run("Fit Spline", "straighten");
getSelectionCoordinates(x, y);
for (i=0; i<x.length; i++)
print(i+" "+x[i]+" "+y[i]);
}
dir=getDirectory("image");
name = "Results";
index = lastIndexOf(name, "\\");
if (index!=-1) name = substring(name, 0, index);
name = name + ".xls"; ///can change xls to csv, txt, etc.
saveAs("Measurements", dir+name);
close();
}
run("Clear Results");
In this macro the polyline generates many many x,y coordinates for each image that may be bulky for the excel file. These coordinates can be listed on one line in the results excel file.
Is there a way you can add manual annotation to the label in the results page?
Also, is there a way you can add pose measurement functions? Like divide the numbers by 2, etc before creating the excel file?
I wrote a macro which saves results of ROI multimeasures to the log window. Since I only wanted specific results (the highest three Raw Integrated Densities), I had my code use getResult("Column Label", row) to get the results out, manipulate them, and then print to the log using print("stuff to print"). At the end, I saved the log as a csv, which one can open in excel. Just use commas to separate
i have a code like this:
Mat img = Highgui.imread(inFile);
Mat templ = Highgui.imread(templateFile);
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
Imgproc.matchTemplate(img, templ, result, Imgproc.TM_CCOEFF);
/////Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
for (int i = 0; i < result_rows; i++)
for (int j = 0; j < result_cols; j++)
if(result.get(i, j)[0]>?)
//match!
I need to parse the input image to find multiple occurrencies of the template image. I want to have a result like this:
result[0][0]= 15%
result[0][1]= 17%
result[x][y]= 47%
If i use TM_COEFF all results are [-xxxxxxxx.xxx,+xxxxxxxx.xxx]
If i use TM_SQDIFF all results are xxxxxxxx.xxx
If i use TM_CCORR all results are xxxxxxxx.xxx
How can i detect a match or a mismatch? What is the right condition into the if?
If i normalized the matrix the application set a value to 1 and i can't detect if the template isn't stored into the image (all mismatch).
Thanks in advance
You can append "_NORMED" to the method names (For instance: CV_TM_COEFF_NORMED in C++; could be slightly different in Java) to get a sensible value for your purpose.
By 'sensible', I mean that you will get values in the range of 0 to 1 which can be multiplied by 100 for your purpose.
Note: For CV_TM_SQDIFF_NORMED, it will be in the range -1 to 0, and you will have to subtract the value from 1 in order to make sense of it, because the lowest value if used in this method.
Tip: you can use the java equivalent of minMaxLoc() in order to get the minimum and maximum values. It's very useful when used in conjunction with matchtemplate.
I believe 'minMaxLoc' that is located inside the class Core.
Here's a C++ implementation:
matchTemplate( input_mat, template_mat, result_mat, method_NORMED );
double minVal, maxVal;
double percentage;
Point minLoc; Point maxLoc;
minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
if( method_NORMED == CV_TM_SQDIFF_NORMED )
{
percentage=1-minVal;
}
else
{
percentage=maxVal;
}
Useful C++ docs:
Match template description along with available methods: http://docs.opencv.org/modules/imgproc/doc/object_detection.html
MinMaxLoc documentation:
http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=minmaxloc#minmaxloc
Another approach will be background differencing. You can observe the distortion.
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
public class BackgroundDifference {
public static void main(String[] arg){
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat model = Highgui.imread("e:\\answers\\template.jpg",Highgui.CV_LOAD_IMAGE_GRAYSCALE);
Mat scene = Highgui.imread("e:\\answers\\front7.jpg",Highgui.CV_LOAD_IMAGE_GRAYSCALE);
Mat diff = new Mat();
Core.absdiff(model,scene,diff);
Imgproc.threshold(diff,diff,15,255,Imgproc.THRESH_BINARY);
int distortion = Core.countNonZero(diff);
System.out.println("distortion:"+distortion);
Highgui.imwrite("e:\\answers\\diff.jpg",diff);
}
}
I am taking a computer graphics class, and I need to work with textures, but I can't use any library to do it. I am stuck on loading the rgb values of the images I need to use (the images can be in any format, jpg, raw, png, etc..) so my question is, which is the easiest way to get the rgb values of an image (of any format) without using any libraries to get this values?? Here is what I found already on the site:
unsigned char *data;
File *file;
file = fopen("image.png", "r");//
data = (unsigned char *)malloc(TH*TV*3); //TH and TV are both 50
fread(data, TH*TV*3, 1, file);
fclose(file);
int i;
for(i=0;i<TH*TV*3;i++){
//suposing I have a struct RGB for the rgb values
RGB.r = data[?];// how do I get the r value
RGB.g = data[?];// how do I get the g value
RGB.b = data[?];// how do I get the b value
}
Thanks
Rather than iterating through every byte that you read in, you want to iterate every pixel which consists of 3 bytes. So replace i++ with i+=3.
for(i=0;i<TH*TV*3;i+=3){
RGB.r = data[i];
RGB.g = data[i+1];
RGB.b = data[i+2];
}
Try to use some framework like OpenCV there are several options to get the colors or to manipulate an image.
Here I found this example code:
cv::Mat img = cv::imread("lenna.png");
for(int i=0; i<img.rows; i++) {
for(int j=0; j<img.cols; j++) {
// You can now access the pixel value with cv::Vec3b
std::cout << img.at<cv::Vec3b>(i,j)[0] << " ";
str::cout << img.at<cv::Vec3b>(i,j)[1] << " ";
str::cout << img.at<cv::Vec3b>(i,j)[2] << std::endl;
}
}
But please note that the code above is not very performance, but the code above should give you an idea how to read the pixels.