How to save my counter results on my image?

Hello all! I am looking to save my counter results from the Point Tool in imagej, onto my actual image with all the counts now. I just want to be able to save the table of the results on the image without having to drag the table on the image and snipping the whole image (table included). Snipping it degrades the quality of the image and I want to avoid that. Anyone know what to do?? Thanks.

Please show us how the final image should look like.

Regards

Herbie

So the final image should look something like this (this was obtained using a snipping tool).

Thanks,

Coral

Coral,

thanks for the illustration but presently I have no idea of how to manage this.

It is possible to get the content and the header of a Results table as text but without formatting and there the problems begin.

If formated text is possible, it could be introduced to your image as overlay, i.e. then it doesn’t destroy the image.

Perhaps someone else is ingenious enough to provide a solution.

Regards

Herbie

1 Like

Herbie,

Thanks for looking into it though!

Coral

Have a look at the Overlay maccro which allows you to draw text over your image. Get your results to print on the overlay from the Results table using getValue(columname, rowindex).

1 Like

[…] which allows you to draw text over your image.

Yes, but it is far from easy to format the column headers and the values. I mentioned this problem in my previous post (did you read it?) and I’m convinced the effort for a corresponding macro, that has to be adapted depending on an individual Results-table, is not worth it.

What would help is a kind of screen shot from within ImageJ (with macro command) of the Results-table, so that the resulting image can be used as an overlay.

Regards

Herbie

Wouldn’t a double loop over the results table allow you to harvest column width and in combination with a monospaced font give you the space required? From the original post I got the impression that there were not too many columns.

1 Like

Go ahead and show us a macro that does this and that is flexible enough to cope with various Results table appearances. If the effort for coding is small enough and worthwhile than you could do in a couple of minutes—no?

Looking forward to seeing your solution

Herbie

Hi Coral,

Your image looks cool :slight_smile: – is it sea lions or something like that?

To simplify the formatting issues, can you give some more details on the type of results you’ll have?

  • Will there always be just 2 counter types? If not, what’s the minimum and maximum # of counter types you’d be likely to have?

  • What’s the size range of your images (length and width in pixels)?

With a few constraints like this, I think your task can be achieved using the methods in the DrawTextWithBackground example.

In particular: Right-justified text with a white background duplicates a table fairly well. I’m thinking you’d draw one string for each column of results.

It is similar to what @eljonco suggested, except that I can’t figure out how to add a background color to an overlay. And the background color is probably needed to make the text legible.

If the number of counter types is not too big, and the image is generally large, you could come up with a way to position the values reliably near the corner as you show in the screenshot.

1 Like

Good day Theresa,

I would tackle the problem by using a separate ImageJ-window to arrange the Results table information and then use this image—after cropping it to its contents—as an overlay for the image in question. This approach should solve the background question.

If there is a convenient way to make column headers and values appear in an acceptable fashion, then we are done.

Regards

Herbie

1 Like

Hi Coral,
This does most of what you want. Further debugging, alignment, filling background further, checking for out-of-window coordinates when too many lines are present in the results, fancy row headers, handling of decimals etc. is left as an exercise.

if(!isOpen("boats.gif")){
  run("Boats (356K)");}
inimg=getImageID();
run("Clear Results");
 makePoint(250, 156);run("Measure");
makePoint(288, 351);run("Measure");
makeRectangle(274, 99, 21, 22);run("Measure");
makeRectangle(440, 227, 21, 22);run("Measure");

roiManager("reset");
drawResults();
roiManager("Set Fill Color", "#1f000000"); // argb, alpha=0.53
roiManager("Show All without labels");
selectWindow("Results");
 
function drawResults(){
	pixelplace=columnStarts();
	point=12;	px=point*2;	 setFont("mono", point); characterheight=px;
 	headings = split(String.getResultsHeadings);
	getDimensions(imageWidth, height, channels, slices, frames);

	textWidth = pixelplace[pixelplace.length-2];
	
	if(textWidth>imageWidth){
		print("printed results will be wider than image");
		return;
	}
	//build in extra checks on image size and number of rows in results table here

	//set headers
	y=0;
	for (col=0; col<lengthOf(headings); col++){
		rcresult=headings[col];
		x=pixelplace[col];
		makeText(rcresult,x,y);
		roiManager("Add", "white");
			
 	}
	//add water
	for (row=0; row<nResults; row++) { 
		y=(1+row)*characterheight;
		for (col=0; col<lengthOf(headings); col++){
			rcresult=toString(getResult(headings[col],row));
			x=pixelplace[col];
			makeText(rcresult,x,y);
			roiManager("Add", "white");
			
 		}
	}
	roiManager("Set Fill Color", "#1f000000"); // argb, alpha=0.53
	roiManager("Show All without labels");
	setOption("Show All", true);
}
function columnStarts(){
	headings = split(String.getResultsHeadings);
	columnWidths=newArray(headings.length+1);
	pipewidth=15;//getStringWidth(" | "); //if you want to print column separators
	columnWidths[0]=pipewidth;
	headings = split(String.getResultsHeadings); 
	for(c=1;c<headings.length;c++){
		cwr=headings[c];
		columnWidth=getStringWidth(cwr);
		for(i=0;i<nResults;i++){
			cwrStr=getResult(headings[c], i);
			cwr=getStringWidth(toString(cwrStr));
			if(cwr>columnWidth){
				columnWidth=cwr;
			}
		}
		newcw=columnWidths[c-1]+columnWidth+pipewidth;
		columnWidths[c]=newcw;
	}
	return columnWidths;
}
1 Like

Thanks for your effort!

This is what I get with your example macro:

boats-2
(Image cropped to the essential part.)

Regards

Herbie

Yes, that makes sense.

Another thought – if we could get the position on screen of the Results window, then we could do a screen capture and crop it, then use as an overlay as you suggest. Unfortunately I’m not sure that this can be done in the macro language.

Yes, Theresa,

that’s what I’ve proposed earlier in this thread:

What would help is a kind of screen shot from within ImageJ (with macro command) of the Results-table, so that the resulting image can be used as an overlay.

However, the screen-shot feature of ImageJ

run("Capture Image");
run("Capture Screen");

doesn’t capture tables in isolation.

Regards

Herbie

Hi tswayne,

Yes, it’s mostly sea lions. To answer your other questions, we are looking to basically count the number of animals, and occasionally there will be other species dispersed in some of these hallout groups, so I would say our counter types could range from 1-7 (ish). The size range for this image in particular is 5568X3712, but that may not always be the case. Unfortunately, this is my first time working with imagej, and I’m not really a coder, so I’m unsure how to implement the information you guys are sending me. Is there anyone to contact on this forum, or imagej help in general, who can assist me on the working coding in for these recommendations you all have? I greatly appreciate it!

Thanks again,
Coral

1 Like

Coral,

please be patient and perhaps wait until monday …

Is there anyone to contact on this forum, or imagej help in general, who can assist me on the working coding […]

We are all volunteers who help via the Forum and ImageJ is open source, not a commercial product.

Regards

Herbie

1 Like

Herbie,

Not a problem, I’m not in a rush. I appreciate any help any volunteers can give. I hope everyone has a great weekend.

Thanks,
Coral

For getting acquainted with macro coding, this post has some great suggestions.

Following the suggestions in an earlier thread, I was able to capture a screenshot of the Results window.

I know it’s not exactly what you need because it grabs the Results, not the counts directly. But it’s hopefully a step in the right direction.

Then a little more code should enable you to place that image as an overlay on the aerial photo, as Herbie suggested.

// ImageJ macro demonstrating how to capture a screenshot of the Results Window

// load a sample image
run("Blobs (25K)");
imageName = getTitle();

// make some dummy measurements
run("Clear Results");
run("Measure");

// use beanshell to get the size of the results table, and position it in a known location
resW = eval("bsh", "rt = ResultsTable.getResultsWindow();resW = rt.getWidth();return resW");
resH = eval("bsh", "rt = ResultsTable.getResultsWindow();resH = rt.getHeight();return resH");
eval("bsh", "rt = ResultsTable.getResultsWindow();rt.setLocation(100, 100)")

// put Results window on top and capture the screen
selectWindow("Results");
run("Capture Screen");

// crop the screenshot to include only the Results window
selectWindow("Screenshot");
makeRectangle(100, 100, resW, resH);
run("Crop");
rename("Results for "+imageName);

That’s because there are decimals in your results and formatting of

cwrStr=getResult(headings[c], i);
cwr=getStringWidth(toString(cwrStr));

somehow doesn’t like decimals. As said: this does most and handling of decimals is left as an exercise.
Together we can :slightly_smiling_face:.
Enjoy your weekend folks.

1 Like