Dear All,
I would like to compare the colocalization of two colors. The problem is that I need to perform a normalization step *per ROI* (this prevents me from using "whole image operations"). Therefore, the idea is to loop over pixels within each ROI, and compare things pixel by pixel (after their normalization). Is there a function that I can call from a macro that would enable me to obtain a pixel array from a ROI? e.g., selectWindow("red"); roiManager("Select", object); ROI2pixels(allPixels); allPixelsRed = allPixels; selectWindow("green"); roiManager("Select", object); ROI2pixels(allPixels); allPixelsGreen = allPixels; ---> then I could normalize each table individually and compare them pixel by pixel. I hope I'll be able to do this in a macro and not have to re-write everything in Java. Many thanks in advance for any pointer. Emmanuel -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hi Emmanuel,
My understanding is that macros don't handle 2D arrays, but the Macro Functions site had a suggestion which I've used to work on ROIs from the roiManager. I used your example and copied a couple of little functions below which I use to generate 1D array from an ROI selection. The appendToArray is from Richard Wheeler at the website indicated and you'll need it in your macro also. Also included is a little function to modify the ROI from a 1D array just in case you need to do so. Were you were looking for something like this or did I misunderstand? Best, George print("\\Clear"); for(j=0;j<roiManager("count");j++){ selectWindow("red.tif"); roiManager("Select", j); getSelectionBounds(xROI0, yROI0, xWidth, yHeight); ROI1DArrayImage1=generate1DArrayFromImageSelection(xROI0, yROI0, xROI0+xWidth, yROI0+yHeight); selectWindow("green.tif"); roiManager("Select", j); getSelectionBounds(xROI0, yROI0, xWidth, yHeight); ROI1DArrayImage2=generate1DArrayFromImageSelection(xROI0, yROI0, xROI0+xWidth, yROI0+yHeight); //I used this part to test with your example /*for(i=0; i<ROI1DArrayImage1.length;i++){ print(ROI1DArrayImage1[i]+" "+ROI1DArrayImage2[i]); } selectWindow("green.tif"); modifyPixelValuesROI(xROI0, yROI0, xROI0+xWidth, yROI0+yHeight,ROI1DArrayImage1); */ } function generate1DArrayFromImageSelection(xStart,yStart,xEnd,yEnd){ ROI1DArray=newArray(); for(i=yStart;i<yEnd;i++){ for(j=xStart;j<xEnd;j++){ ROI1DArray=appendToArray(getPixel(j,i),ROI1DArray); } } return ROI1DArray; } function modifyPixelValuesROI(xStart,yStart,xEnd,yEnd,ROI1DArray){ for(i=yStart;i<yEnd;i++){ for(j=xStart;j<xEnd;j++){ setPixel(j,i,ROI1DArray[(j-xStart)+((i-yStart)*(xEnd-xStart))]); } } } function appendToArray(value, array) { temparray=newArray(lengthOf(array)+1); for(i=0;i<lengthOf(array);i++){ temparray[i]=array[i]; } temparray[lengthOf(temparray)-1]=value; array=temparray; return array; } //appendToArray function borrowed from //http://www.richardwheeler.net/contentpages/text.php?gallery=ImageJ_Macros&file=Array_Tools&type=ijm On Mar 17, 2013, at 2:31 PM, Emmanuel Levy wrote: > Dear All, > > I would like to compare the colocalization of two colors. The problem > is that I need to perform a normalization step *per ROI* (this > prevents me from using "whole image operations"). > > Therefore, the idea is to loop over pixels within each ROI, and > compare things pixel by pixel (after their normalization). > > Is there a function that I can call from a macro that would enable me > to obtain a pixel array from a ROI? > > e.g., > selectWindow("red"); > roiManager("Select", object); > ROI2pixels(allPixels); > allPixelsRed = allPixels; > > selectWindow("green"); > roiManager("Select", object); > ROI2pixels(allPixels); > allPixelsGreen = allPixels; > > ---> then I could normalize each table individually and compare them > pixel by pixel. > > I hope I'll be able to do this in a macro and not have to re-write > everything in Java. > > Many thanks in advance for any pointer. > > Emmanuel > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html George H. Patterson Building 13 3E33 13 South Drive Biophotonics Section National Institute of Biomedical Imaging and Bioengineering National Institutes of Health Bethesda, MD 20892 Office: 301-443-0241 Fax: 301-496-6608 [hidden email] -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hello Georges,
Thanks a lot for your answer - a 1D array is exactly what I need: all I wish to do is pixel-by-pixel comparisons of the same ROI across different images. The one thing I'm wondering about is that my ROIs are not rectangles but the function below seems to scan a rectangle (corresponding to the bounding box). Another possible concern might be that the "append" function is likely slow (because it duplicates an array to append an element to it) and I would need to apply it on millions of ROIs :-/ In practice it seems that a "simpler" version of a getHistogram function would do the job, i.e., simply return an array of pixel values instead of counts. The only important thing would be that the pixel are read in the same way on all images for a given ROI so that the arrays obtained can be compared to each other. Actually, I'm not sure how to do this but is there a way to create such a customized function inspired by getHistogram and re-use it? I guess I would need to look at the java file of a plugin, look at the code of "getHistogram" (where could I find it?) and make a new function that I could call from the macro I guess? Many thanks for any input on this! All the best, Emmanuel On 20 March 2013 17:26, George Patterson <[hidden email]> wrote: > Hi Emmanuel, > My understanding is that macros don't handle 2D arrays, but the Macro Functions site had a suggestion which I've used to work on ROIs from the roiManager. > I used your example and copied a couple of little functions below which I use to generate 1D array from an ROI selection. The appendToArray is from Richard Wheeler at the website indicated and you'll need it in your macro also. > Also included is a little function to modify the ROI from a 1D array just in case you need to do so. > Were you were looking for something like this or did I misunderstand? > Best, > George > > > print("\\Clear"); > for(j=0;j<roiManager("count");j++){ > selectWindow("red.tif"); > roiManager("Select", j); > getSelectionBounds(xROI0, yROI0, xWidth, yHeight); > ROI1DArrayImage1=generate1DArrayFromImageSelection(xROI0, yROI0, xROI0+xWidth, yROI0+yHeight); > > selectWindow("green.tif"); > roiManager("Select", j); > getSelectionBounds(xROI0, yROI0, xWidth, yHeight); > ROI1DArrayImage2=generate1DArrayFromImageSelection(xROI0, yROI0, xROI0+xWidth, yROI0+yHeight); > > //I used this part to test with your example > /*for(i=0; i<ROI1DArrayImage1.length;i++){ > print(ROI1DArrayImage1[i]+" "+ROI1DArrayImage2[i]); > } > selectWindow("green.tif"); > modifyPixelValuesROI(xROI0, yROI0, xROI0+xWidth, yROI0+yHeight,ROI1DArrayImage1); > */ > > } > > > > function generate1DArrayFromImageSelection(xStart,yStart,xEnd,yEnd){ > ROI1DArray=newArray(); > for(i=yStart;i<yEnd;i++){ > for(j=xStart;j<xEnd;j++){ > ROI1DArray=appendToArray(getPixel(j,i),ROI1DArray); > } > } > return ROI1DArray; > } > > function modifyPixelValuesROI(xStart,yStart,xEnd,yEnd,ROI1DArray){ > for(i=yStart;i<yEnd;i++){ > for(j=xStart;j<xEnd;j++){ > setPixel(j,i,ROI1DArray[(j-xStart)+((i-yStart)*(xEnd-xStart))]); > } > } > } > > function appendToArray(value, array) { > temparray=newArray(lengthOf(array)+1); > for(i=0;i<lengthOf(array);i++){ > temparray[i]=array[i]; > } > temparray[lengthOf(temparray)-1]=value; > array=temparray; > return array; > } > //appendToArray function borrowed from > //http://www.richardwheeler.net/contentpages/text.php?gallery=ImageJ_Macros&file=Array_Tools&type=ijm > > > On Mar 17, 2013, at 2:31 PM, Emmanuel Levy wrote: > >> Dear All, >> >> I would like to compare the colocalization of two colors. The problem >> is that I need to perform a normalization step *per ROI* (this >> prevents me from using "whole image operations"). >> >> Therefore, the idea is to loop over pixels within each ROI, and >> compare things pixel by pixel (after their normalization). >> >> Is there a function that I can call from a macro that would enable me >> to obtain a pixel array from a ROI? >> >> e.g., >> selectWindow("red"); >> roiManager("Select", object); >> ROI2pixels(allPixels); >> allPixelsRed = allPixels; >> >> selectWindow("green"); >> roiManager("Select", object); >> ROI2pixels(allPixels); >> allPixelsGreen = allPixels; >> >> ---> then I could normalize each table individually and compare them >> pixel by pixel. >> >> I hope I'll be able to do this in a macro and not have to re-write >> everything in Java. >> >> Many thanks in advance for any pointer. >> >> Emmanuel >> >> -- >> ImageJ mailing list: http://imagej.nih.gov/ij/list.html > > George H. Patterson > Building 13 3E33 > 13 South Drive > Biophotonics Section > National Institute of Biomedical Imaging and Bioengineering > National Institutes of Health > Bethesda, MD 20892 > Office: 301-443-0241 > Fax: 301-496-6608 > [hidden email] > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hi Emmanuel,
On Mar 20, 2013, at 12:52 PM, Emmanuel Levy wrote: > Hello Georges, > > Thanks a lot for your answer - a 1D array is exactly what I need: all > I wish to do is pixel-by-pixel comparisons of the same ROI across > different images. > > The one thing I'm wondering about is that my ROIs are not rectangles > but the function below seems to scan a rectangle (corresponding to the > bounding box). Yes, these are for rectangular selections, which are obviously easier to implement. However, your response prompted me to try developing a function for non-rectangular ROIs for use in my own analyses. See the scripts below. > Another possible concern might be that the "append" > function is likely slow (because it duplicates an array to append an > element to it) and I would need to apply it on millions of ROIs :-/ Certainly a valid concern and the version below does not use the appendToArray function. It might still be pretty slow for you. It requires making new images for each ROI and this is probably a really slow step. I estimate it can generate arrays from about 500 ROIs per second when I block out the print and modify parts of my little test script. This will likely depend on ROI size. I haven't tested that part yet. One potential issue is that it assumes you have no 0 pixel values within your ROIs. So bear this in mind if you try this macro. > > In practice it seems that a "simpler" version of a getHistogram > function would do the job, i.e., simply return an array of pixel > values instead of counts. The only important thing would be that the > pixel are read in the same way on all images for a given ROI so that > the arrays obtained can be compared to each other. I thought about that approach, but I couldn't figure out how to sneak around the binning and sorting of the pixel values. > > Actually, I'm not sure how to do this but is there a way to create > such a customized function inspired by getHistogram and re-use it? I > guess I would need to look at the java file of a plugin, look at the > code of "getHistogram" (where could I find it?) and make a new > function that I could call from the macro I guess? You can browse the source code here http://rsb.info.nih.gov/ij/developer/index.html to see how the histogram functions work. Jay Unruh has a plugin (http://research.stowers.org/imagejplugins) that he kindly shares with the ImageJ community. And/or see this thread (http://imagej.1557.n6.nabble.com/Creating-a-histogram-image-custom-data-td3681992.html#a3681997). > > Many thanks for any input on this! > > All the best, > > Emmanuel > Best regards, George print("\\Clear"); startTime=getTime(); setBatchMode(true); for(j=0;j<roiManager("count");j++){ selectWindow("red.tif"); ImageID=getImageID(); roiManager("Select", j); getSelectionCoordinates(xCoordinatesArray, yCoordinatesArray); ROIType=selectionType(); ROI1DArrayImage1=generate1DPixelArrayFromCoordinates(xCoordinatesArray, yCoordinatesArray, ROIType, ImageID); selectWindow("green.tif"); ImageID=getImageID(); ROI1DArrayImage2=generate1DPixelArrayFromCoordinates(xCoordinatesArray, yCoordinatesArray, ROIType, ImageID); //I used this part to test with your example /* for(i=0; i<ROI1DArrayImage1.length;i++){ print(ROI1DArrayImage1[i]+" "+ROI1DArrayImage2[i]); } selectWindow("green.tif"); ImageID=getImageID(); xROIArray=getXpixelCoordinates(xCoordinatesArray, yCoordinatesArray, ROIType, ImageID); yROIArray=getYpixelCoordinates(xCoordinatesArray, yCoordinatesArray, ROIType, ImageID); modifyPixelValuesROI(xROIArray, yROIArray, ROI1DArrayImage1,ImageID); */ } print("ROI number= "+roiManager("count")); stopTime=getTime(); timeRequiredMin=(stopTime-startTime)/(1000*60); timeRequiredSec=(stopTime-startTime)/(1000); timeRequiredMinutes=floor(timeRequiredMin); timeRequiredSeconds=timeRequiredSec-(timeRequiredMinutes*60); print("time required ="+timeRequiredMinutes+" minutes "+timeRequiredSeconds+" seconds"); //****************************functions******************* function getXpixelCoordinates(xArray, yArray, roiType, imageID){ selectionPixelCount=0; BatchModeYorN=is("Batch Mode"); if(BatchModeYorN==false){ setBatchMode(true); } selectImage(imageID); imageType=bitDepth(); makeSelection(roiType, xArray, yArray); getSelectionBounds(x0,y0,xWidth,yHeight); ROI1DArray=newArray(xWidth*yHeight); ROI1DArrayXcoordinates=newArray(xWidth*yHeight); ROI1DArrayYcoordinates=newArray(xWidth*yHeight); run("Copy"); //run("Internal Clipboard", "black"); newImage("TempROI",imageType+" black",xWidth,yHeight,1); run("Paste"); for(y=0;y<yHeight;y++){ for(x=0;x<xWidth;x++){ pixValue=getPixel(x,y); if(pixValue!=0){ ROI1DArrayXcoordinates[selectionPixelCount]=x+x0; ROI1DArrayYcoordinates[selectionPixelCount]=y+y0; ROI1DArray[selectionPixelCount]=pixValue; selectionPixelCount=selectionPixelCount+1; } } } close(); ROI1DArrayXcoordinates=Array.trim(ROI1DArrayXcoordinates,selectionPixelCount); ROI1DArrayYcoordinates=Array.trim(ROI1DArrayYcoordinates,selectionPixelCount); ROI1DArray=Array.trim(ROI1DArray,selectionPixelCount); setBatchMode(BatchModeYorN); return ROI1DArrayXcoordinates; } function getYpixelCoordinates(xArray, yArray, roiType, imageID){ selectionPixelCount=0; BatchModeYorN=is("Batch Mode"); if(BatchModeYorN==false){ setBatchMode(true); } selectImage(imageID); imageType=bitDepth(); makeSelection(roiType, xArray, yArray); getSelectionBounds(x0,y0,xWidth,yHeight); ROI1DArray=newArray(xWidth*yHeight); ROI1DArrayXcoordinates=newArray(xWidth*yHeight); ROI1DArrayYcoordinates=newArray(xWidth*yHeight); run("Copy"); //run("Internal Clipboard", "black"); newImage("TempROI",imageType+" black",xWidth,yHeight,1); run("Paste"); for(y=0;y<yHeight;y++){ for(x=0;x<xWidth;x++){ pixValue=getPixel(x,y); if(pixValue!=0){ ROI1DArrayXcoordinates[selectionPixelCount]=x+x0; ROI1DArrayYcoordinates[selectionPixelCount]=y+y0; ROI1DArray[selectionPixelCount]=pixValue; selectionPixelCount=selectionPixelCount+1; } } } close(); ROI1DArrayXcoordinates=Array.trim(ROI1DArrayXcoordinates,selectionPixelCount); ROI1DArrayYcoordinates=Array.trim(ROI1DArrayYcoordinates,selectionPixelCount); ROI1DArray=Array.trim(ROI1DArray,selectionPixelCount); setBatchMode(BatchModeYorN); return ROI1DArrayYcoordinates; } function generate1DPixelArrayFromCoordinates(xArray, yArray, roiType, imageID){ selectionPixelCount=0; BatchModeYorN=is("Batch Mode"); if(BatchModeYorN==false){ setBatchMode(true); } selectImage(imageID); imageType=bitDepth(); makeSelection(roiType, xArray, yArray); getSelectionBounds(x0,y0,xWidth,yHeight); ROI1DArray=newArray(xWidth*yHeight); ROI1DArrayXcoordinates=newArray(xWidth*yHeight); ROI1DArrayYcoordinates=newArray(xWidth*yHeight); run("Copy"); //run("Internal Clipboard", "black"); newImage("TempROI",imageType+" black",xWidth,yHeight,1); run("Paste"); for(y=0;y<yHeight;y++){ for(x=0;x<xWidth;x++){ pixValue=getPixel(x,y); if(pixValue!=0){ ROI1DArrayXcoordinates[selectionPixelCount]=x+x0; ROI1DArrayYcoordinates[selectionPixelCount]=y+y0; ROI1DArray[selectionPixelCount]=pixValue; selectionPixelCount=selectionPixelCount+1; } } } close(); ROI1DArrayXcoordinates=Array.trim(ROI1DArrayXcoordinates,selectionPixelCount); ROI1DArrayYcoordinates=Array.trim(ROI1DArrayYcoordinates,selectionPixelCount); ROI1DArray=Array.trim(ROI1DArray,selectionPixelCount); setBatchMode(BatchModeYorN); return ROI1DArray; } function modifyPixelValuesROI(xArray, yArray, ROI1DArray, imageID){ BatchModeYorN=is("Batch Mode"); if(BatchModeYorN==false){ setBatchMode(true); } selectImage(imageID); for(i=0;i<ROI1DArray.length;i++){ setPixel(xArray[i],yArray[i],ROI1DArray[i]); } } > > > > > On 20 March 2013 17:26, George Patterson <[hidden email]> wrote: >> Hi Emmanuel, >> My understanding is that macros don't handle 2D arrays, but the Macro Functions site had a suggestion which I've used to work on ROIs from the roiManager. >> I used your example and copied a couple of little functions below which I use to generate 1D array from an ROI selection. The appendToArray is from Richard Wheeler at the website indicated and you'll need it in your macro also. >> Also included is a little function to modify the ROI from a 1D array just in case you need to do so. >> Were you were looking for something like this or did I misunderstand? >> Best, >> George >> >> >> print("\\Clear"); >> for(j=0;j<roiManager("count");j++){ >> selectWindow("red.tif"); >> roiManager("Select", j); >> getSelectionBounds(xROI0, yROI0, xWidth, yHeight); >> ROI1DArrayImage1=generate1DArrayFromImageSelection(xROI0, yROI0, xROI0+xWidth, yROI0+yHeight); >> >> selectWindow("green.tif"); >> roiManager("Select", j); >> getSelectionBounds(xROI0, yROI0, xWidth, yHeight); >> ROI1DArrayImage2=generate1DArrayFromImageSelection(xROI0, yROI0, xROI0+xWidth, yROI0+yHeight); >> >> //I used this part to test with your example >> /*for(i=0; i<ROI1DArrayImage1.length;i++){ >> print(ROI1DArrayImage1[i]+" "+ROI1DArrayImage2[i]); >> } >> selectWindow("green.tif"); >> modifyPixelValuesROI(xROI0, yROI0, xROI0+xWidth, yROI0+yHeight,ROI1DArrayImage1); >> */ >> >> } >> >> >> >> function generate1DArrayFromImageSelection(xStart,yStart,xEnd,yEnd){ >> ROI1DArray=newArray(); >> for(i=yStart;i<yEnd;i++){ >> for(j=xStart;j<xEnd;j++){ >> ROI1DArray=appendToArray(getPixel(j,i),ROI1DArray); >> } >> } >> return ROI1DArray; >> } >> >> function modifyPixelValuesROI(xStart,yStart,xEnd,yEnd,ROI1DArray){ >> for(i=yStart;i<yEnd;i++){ >> for(j=xStart;j<xEnd;j++){ >> setPixel(j,i,ROI1DArray[(j-xStart)+((i-yStart)*(xEnd-xStart))]); >> } >> } >> } >> >> function appendToArray(value, array) { >> temparray=newArray(lengthOf(array)+1); >> for(i=0;i<lengthOf(array);i++){ >> temparray[i]=array[i]; >> } >> temparray[lengthOf(temparray)-1]=value; >> array=temparray; >> return array; >> } >> //appendToArray function borrowed from >> //http://www.richardwheeler.net/contentpages/text.php?gallery=ImageJ_Macros&file=Array_Tools&type=ijm >> >> >> On Mar 17, 2013, at 2:31 PM, Emmanuel Levy wrote: >> >>> Dear All, >>> >>> I would like to compare the colocalization of two colors. The problem >>> is that I need to perform a normalization step *per ROI* (this >>> prevents me from using "whole image operations"). >>> >>> Therefore, the idea is to loop over pixels within each ROI, and >>> compare things pixel by pixel (after their normalization). >>> >>> Is there a function that I can call from a macro that would enable me >>> to obtain a pixel array from a ROI? >>> >>> e.g., >>> selectWindow("red"); >>> roiManager("Select", object); >>> ROI2pixels(allPixels); >>> allPixelsRed = allPixels; >>> >>> selectWindow("green"); >>> roiManager("Select", object); >>> ROI2pixels(allPixels); >>> allPixelsGreen = allPixels; >>> >>> ---> then I could normalize each table individually and compare them >>> pixel by pixel. >>> >>> I hope I'll be able to do this in a macro and not have to re-write >>> everything in Java. >>> >>> Many thanks in advance for any pointer. >>> >>> Emmanuel >>> >>> -- >>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html >> >> George H. Patterson >> Building 13 3E33 >> 13 South Drive >> Biophotonics Section >> National Institute of Biomedical Imaging and Bioengineering >> National Institutes of Health >> Bethesda, MD 20892 >> Office: 301-443-0241 >> Fax: 301-496-6608 >> [hidden email] >> >> -- >> ImageJ mailing list: http://imagej.nih.gov/ij/list.html > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html George H. Patterson Building 13 3E33 13 South Drive Biophotonics Section National Institute of Biomedical Imaging and Bioengineering National Institutes of Health Bethesda, MD 20892 Office: 301-443-0241 Fax: 301-496-6608 [hidden email] -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hello George,
Thank you so much for the code, that's going to be really useful. Thank you very much also for the link http://rsb.info.nih.gov/ij/developer/index.html I cannot believe I did not pay more attention to it before. Actually, I saw the following function (in the javascript examples): http://rsb.info.nih.gov/ij/macros/js/ListPixelsInROI.js which is quite close to what I was thinking about, with the following trick: mask.getPixel(x,y)!=0 to see if a pixel is in the ROI. This is javascript, but I suppose that it points to the places where to look for the relevant code in Java. I will definitely look more into this this week! All the best, Emmanuel On 21 March 2013 18:09, George Patterson <[hidden email]> wrote: > Hi Emmanuel, > > On Mar 20, 2013, at 12:52 PM, Emmanuel Levy wrote: > >> Hello Georges, >> >> Thanks a lot for your answer - a 1D array is exactly what I need: all >> I wish to do is pixel-by-pixel comparisons of the same ROI across >> different images. >> >> The one thing I'm wondering about is that my ROIs are not rectangles >> but the function below seems to scan a rectangle (corresponding to the >> bounding box). > Yes, these are for rectangular selections, which are obviously easier to implement. However, your response prompted me to try developing a function for non-rectangular ROIs for use in my own analyses. See the scripts below. >> Another possible concern might be that the "append" >> function is likely slow (because it duplicates an array to append an >> element to it) and I would need to apply it on millions of ROIs :-/ > Certainly a valid concern and the version below does not use the appendToArray function. It might still be pretty slow for you. It requires making new images for each ROI and this is probably a really slow step. > I estimate it can generate arrays from about 500 ROIs per second when I block out the print and modify parts of my little test script. This will likely depend on ROI size. I haven't tested that part yet. > One potential issue is that it assumes you have no 0 pixel values within your ROIs. So bear this in mind if you try this macro. >> >> In practice it seems that a "simpler" version of a getHistogram >> function would do the job, i.e., simply return an array of pixel >> values instead of counts. The only important thing would be that the >> pixel are read in the same way on all images for a given ROI so that >> the arrays obtained can be compared to each other. > I thought about that approach, but I couldn't figure out how to sneak around the binning and sorting of the pixel values. >> >> Actually, I'm not sure how to do this but is there a way to create >> such a customized function inspired by getHistogram and re-use it? I >> guess I would need to look at the java file of a plugin, look at the >> code of "getHistogram" (where could I find it?) and make a new >> function that I could call from the macro I guess? > You can browse the source code here http://rsb.info.nih.gov/ij/developer/index.html to see how the histogram functions work. > Jay Unruh has a plugin (http://research.stowers.org/imagejplugins) that he kindly shares with the ImageJ community. > And/or see this thread (http://imagej.1557.n6.nabble.com/Creating-a-histogram-image-custom-data-td3681992.html#a3681997). >> >> Many thanks for any input on this! >> >> All the best, >> >> Emmanuel >> > > Best regards, > George > > > print("\\Clear"); > startTime=getTime(); > setBatchMode(true); > for(j=0;j<roiManager("count");j++){ > selectWindow("red.tif"); > ImageID=getImageID(); > roiManager("Select", j); > getSelectionCoordinates(xCoordinatesArray, yCoordinatesArray); > ROIType=selectionType(); > ROI1DArrayImage1=generate1DPixelArrayFromCoordinates(xCoordinatesArray, yCoordinatesArray, ROIType, ImageID); > > selectWindow("green.tif"); > ImageID=getImageID(); > ROI1DArrayImage2=generate1DPixelArrayFromCoordinates(xCoordinatesArray, yCoordinatesArray, ROIType, ImageID); > > //I used this part to test with your example > /* > for(i=0; i<ROI1DArrayImage1.length;i++){ > print(ROI1DArrayImage1[i]+" "+ROI1DArrayImage2[i]); > } > > selectWindow("green.tif"); > ImageID=getImageID(); > xROIArray=getXpixelCoordinates(xCoordinatesArray, yCoordinatesArray, ROIType, ImageID); > yROIArray=getYpixelCoordinates(xCoordinatesArray, yCoordinatesArray, ROIType, ImageID); > > modifyPixelValuesROI(xROIArray, yROIArray, ROI1DArrayImage1,ImageID); > */ > > } > print("ROI number= "+roiManager("count")); > stopTime=getTime(); > timeRequiredMin=(stopTime-startTime)/(1000*60); > timeRequiredSec=(stopTime-startTime)/(1000); > timeRequiredMinutes=floor(timeRequiredMin); > timeRequiredSeconds=timeRequiredSec-(timeRequiredMinutes*60); > print("time required ="+timeRequiredMinutes+" minutes "+timeRequiredSeconds+" seconds"); > > > //****************************functions******************* > function getXpixelCoordinates(xArray, yArray, roiType, imageID){ > selectionPixelCount=0; > BatchModeYorN=is("Batch Mode"); > if(BatchModeYorN==false){ > setBatchMode(true); > } > selectImage(imageID); > imageType=bitDepth(); > makeSelection(roiType, xArray, yArray); > getSelectionBounds(x0,y0,xWidth,yHeight); > ROI1DArray=newArray(xWidth*yHeight); > ROI1DArrayXcoordinates=newArray(xWidth*yHeight); > ROI1DArrayYcoordinates=newArray(xWidth*yHeight); > run("Copy"); > //run("Internal Clipboard", "black"); > newImage("TempROI",imageType+" black",xWidth,yHeight,1); > run("Paste"); > for(y=0;y<yHeight;y++){ > for(x=0;x<xWidth;x++){ > pixValue=getPixel(x,y); > if(pixValue!=0){ > ROI1DArrayXcoordinates[selectionPixelCount]=x+x0; > ROI1DArrayYcoordinates[selectionPixelCount]=y+y0; > ROI1DArray[selectionPixelCount]=pixValue; > selectionPixelCount=selectionPixelCount+1; > } > } > } > close(); > ROI1DArrayXcoordinates=Array.trim(ROI1DArrayXcoordinates,selectionPixelCount); > ROI1DArrayYcoordinates=Array.trim(ROI1DArrayYcoordinates,selectionPixelCount); > ROI1DArray=Array.trim(ROI1DArray,selectionPixelCount); > setBatchMode(BatchModeYorN); > return ROI1DArrayXcoordinates; > } > > function getYpixelCoordinates(xArray, yArray, roiType, imageID){ > selectionPixelCount=0; > BatchModeYorN=is("Batch Mode"); > if(BatchModeYorN==false){ > setBatchMode(true); > } > selectImage(imageID); > imageType=bitDepth(); > makeSelection(roiType, xArray, yArray); > getSelectionBounds(x0,y0,xWidth,yHeight); > ROI1DArray=newArray(xWidth*yHeight); > ROI1DArrayXcoordinates=newArray(xWidth*yHeight); > ROI1DArrayYcoordinates=newArray(xWidth*yHeight); > run("Copy"); > //run("Internal Clipboard", "black"); > newImage("TempROI",imageType+" black",xWidth,yHeight,1); > run("Paste"); > for(y=0;y<yHeight;y++){ > for(x=0;x<xWidth;x++){ > pixValue=getPixel(x,y); > if(pixValue!=0){ > ROI1DArrayXcoordinates[selectionPixelCount]=x+x0; > ROI1DArrayYcoordinates[selectionPixelCount]=y+y0; > ROI1DArray[selectionPixelCount]=pixValue; > selectionPixelCount=selectionPixelCount+1; > } > } > } > close(); > ROI1DArrayXcoordinates=Array.trim(ROI1DArrayXcoordinates,selectionPixelCount); > ROI1DArrayYcoordinates=Array.trim(ROI1DArrayYcoordinates,selectionPixelCount); > ROI1DArray=Array.trim(ROI1DArray,selectionPixelCount); > setBatchMode(BatchModeYorN); > return ROI1DArrayYcoordinates; > } > > > function generate1DPixelArrayFromCoordinates(xArray, yArray, roiType, imageID){ > selectionPixelCount=0; > BatchModeYorN=is("Batch Mode"); > if(BatchModeYorN==false){ > setBatchMode(true); > } > selectImage(imageID); > imageType=bitDepth(); > makeSelection(roiType, xArray, yArray); > getSelectionBounds(x0,y0,xWidth,yHeight); > ROI1DArray=newArray(xWidth*yHeight); > ROI1DArrayXcoordinates=newArray(xWidth*yHeight); > ROI1DArrayYcoordinates=newArray(xWidth*yHeight); > run("Copy"); > //run("Internal Clipboard", "black"); > newImage("TempROI",imageType+" black",xWidth,yHeight,1); > run("Paste"); > for(y=0;y<yHeight;y++){ > for(x=0;x<xWidth;x++){ > pixValue=getPixel(x,y); > if(pixValue!=0){ > ROI1DArrayXcoordinates[selectionPixelCount]=x+x0; > ROI1DArrayYcoordinates[selectionPixelCount]=y+y0; > ROI1DArray[selectionPixelCount]=pixValue; > selectionPixelCount=selectionPixelCount+1; > } > } > } > close(); > ROI1DArrayXcoordinates=Array.trim(ROI1DArrayXcoordinates,selectionPixelCount); > ROI1DArrayYcoordinates=Array.trim(ROI1DArrayYcoordinates,selectionPixelCount); > ROI1DArray=Array.trim(ROI1DArray,selectionPixelCount); > setBatchMode(BatchModeYorN); > return ROI1DArray; > } > > function modifyPixelValuesROI(xArray, yArray, ROI1DArray, imageID){ > BatchModeYorN=is("Batch Mode"); > if(BatchModeYorN==false){ > setBatchMode(true); > } > selectImage(imageID); > for(i=0;i<ROI1DArray.length;i++){ > setPixel(xArray[i],yArray[i],ROI1DArray[i]); > } > } > > >> >> >> >> >> On 20 March 2013 17:26, George Patterson <[hidden email]> wrote: >>> Hi Emmanuel, >>> My understanding is that macros don't handle 2D arrays, but the Macro Functions site had a suggestion which I've used to work on ROIs from the roiManager. >>> I used your example and copied a couple of little functions below which I use to generate 1D array from an ROI selection. The appendToArray is from Richard Wheeler at the website indicated and you'll need it in your macro also. >>> Also included is a little function to modify the ROI from a 1D array just in case you need to do so. >>> Were you were looking for something like this or did I misunderstand? >>> Best, >>> George >>> >>> >>> print("\\Clear"); >>> for(j=0;j<roiManager("count");j++){ >>> selectWindow("red.tif"); >>> roiManager("Select", j); >>> getSelectionBounds(xROI0, yROI0, xWidth, yHeight); >>> ROI1DArrayImage1=generate1DArrayFromImageSelection(xROI0, yROI0, xROI0+xWidth, yROI0+yHeight); >>> >>> selectWindow("green.tif"); >>> roiManager("Select", j); >>> getSelectionBounds(xROI0, yROI0, xWidth, yHeight); >>> ROI1DArrayImage2=generate1DArrayFromImageSelection(xROI0, yROI0, xROI0+xWidth, yROI0+yHeight); >>> >>> //I used this part to test with your example >>> /*for(i=0; i<ROI1DArrayImage1.length;i++){ >>> print(ROI1DArrayImage1[i]+" "+ROI1DArrayImage2[i]); >>> } >>> selectWindow("green.tif"); >>> modifyPixelValuesROI(xROI0, yROI0, xROI0+xWidth, yROI0+yHeight,ROI1DArrayImage1); >>> */ >>> >>> } >>> >>> >>> >>> function generate1DArrayFromImageSelection(xStart,yStart,xEnd,yEnd){ >>> ROI1DArray=newArray(); >>> for(i=yStart;i<yEnd;i++){ >>> for(j=xStart;j<xEnd;j++){ >>> ROI1DArray=appendToArray(getPixel(j,i),ROI1DArray); >>> } >>> } >>> return ROI1DArray; >>> } >>> >>> function modifyPixelValuesROI(xStart,yStart,xEnd,yEnd,ROI1DArray){ >>> for(i=yStart;i<yEnd;i++){ >>> for(j=xStart;j<xEnd;j++){ >>> setPixel(j,i,ROI1DArray[(j-xStart)+((i-yStart)*(xEnd-xStart))]); >>> } >>> } >>> } >>> >>> function appendToArray(value, array) { >>> temparray=newArray(lengthOf(array)+1); >>> for(i=0;i<lengthOf(array);i++){ >>> temparray[i]=array[i]; >>> } >>> temparray[lengthOf(temparray)-1]=value; >>> array=temparray; >>> return array; >>> } >>> //appendToArray function borrowed from >>> //http://www.richardwheeler.net/contentpages/text.php?gallery=ImageJ_Macros&file=Array_Tools&type=ijm >>> >>> >>> On Mar 17, 2013, at 2:31 PM, Emmanuel Levy wrote: >>> >>>> Dear All, >>>> >>>> I would like to compare the colocalization of two colors. The problem >>>> is that I need to perform a normalization step *per ROI* (this >>>> prevents me from using "whole image operations"). >>>> >>>> Therefore, the idea is to loop over pixels within each ROI, and >>>> compare things pixel by pixel (after their normalization). >>>> >>>> Is there a function that I can call from a macro that would enable me >>>> to obtain a pixel array from a ROI? >>>> >>>> e.g., >>>> selectWindow("red"); >>>> roiManager("Select", object); >>>> ROI2pixels(allPixels); >>>> allPixelsRed = allPixels; >>>> >>>> selectWindow("green"); >>>> roiManager("Select", object); >>>> ROI2pixels(allPixels); >>>> allPixelsGreen = allPixels; >>>> >>>> ---> then I could normalize each table individually and compare them >>>> pixel by pixel. >>>> >>>> I hope I'll be able to do this in a macro and not have to re-write >>>> everything in Java. >>>> >>>> Many thanks in advance for any pointer. >>>> >>>> Emmanuel >>>> >>>> -- >>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html >>> >>> George H. Patterson >>> Building 13 3E33 >>> 13 South Drive >>> Biophotonics Section >>> National Institute of Biomedical Imaging and Bioengineering >>> National Institutes of Health >>> Bethesda, MD 20892 >>> Office: 301-443-0241 >>> Fax: 301-496-6608 >>> [hidden email] >>> >>> -- >>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html >> >> -- >> ImageJ mailing list: http://imagej.nih.gov/ij/list.html > > George H. Patterson > Building 13 3E33 > 13 South Drive > Biophotonics Section > National Institute of Biomedical Imaging and Bioengineering > National Institutes of Health > Bethesda, MD 20892 > Office: 301-443-0241 > Fax: 301-496-6608 > [hidden email] > > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |