bugGNU Octave - Bugs: bug #56958, [octave forge] (image)...

 
 

bug #56958: [octave forge] (image) Implementation of insertText

Submitter:  Guillaume <gyom>
Submitted:  Fri 27 Sep 2019 04:18:11 PM UTC
   
 
Category:  Octave Package Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Feature Request
Status:  In Progress Assigned to:  None
Originator Name:  Guillaume Open/Closed:  * Open
Release:  * other Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Tue 08 Dec 2020 05:31:16 PM UTC, comment #14: 

It's that there is a problems in lines 110-115


  if (ismatrix (img))
    rgb = repmat (img, [1 1 3]);
  endif
  if (isinteger (img))
    rgb = double (img) / 255;
  endif


Should be something like:


  if (ismatrix (img))
    rgb = repmat (img, [1 1 3]);
  else
    rgb = img;
  endif

  if (isinteger (img))
    rgb = double (rgb) / 255;
  endif



Avinoam Kalma <avinoam>
Group Member
Mon 07 Dec 2020 10:23:12 AM UTC, comment #13: 

Thanks Pantxo! I was just wondering whether a more "generic" user-level access to the text_renderer class could be useful in other functions (FontWeight, Interpreter, ...).

I attach where I am so far with insertText.m. I use padarray() so it would indeed probably fit in the image processing package.
Looking back, I should perhaps have used inputParser, struct instead of containers.Map and there might be some functions from the image processing package that could be reused to shorten the code here but I am not too familiar with them.
I added a subfunction str2rgb(): perhaps color_values::str2rgb from libinterp/corefcn/graphics.cc could be made accessible from M-code?

(file #50412)

Guillaume <gyom>
Sun 06 Dec 2020 10:44:09 PM UTC, comment #12: 

@gyom: You can use the function asis. The work on the latex interpreter is independent and won't change the interface to the text_renderer class.
As for bbox, it will always be [0 0 columns rows], unless you specify the vertical and/or horizontal alignment, in which case the 2 first elements (the anchor) will change.

Pantxo Diribarne <pantxo>
Group Member
Sun 06 Dec 2020 08:11:27 PM UTC, comment #11: 

@Avinoam: I've already been working on this and will share a file tomorrow.

@Pantxo: Thank you - that's exactly what I was looking for. Shall we use _text_to_pixels_ as is or should it be part of something else, e.g. will your work on the latex interpreter be merged into this? Also, can bbox be figured out from size(RGBA) or are there situations where they can differ?

Guillaume <gyom>
Sun 06 Dec 2020 07:57:57 PM UTC, comment #10: 

Thanks @Pantxo for the code.
I will try to wrap it into a function.

Avinoam Kalma <avinoam>
Group Member
Fri 04 Dec 2020 09:59:34 PM UTC, comment #9: 

If you want to go that route, I attached a file that should get you going.

Basically _text_to_pixels_ returns a  4-by-M-by-N RGBA unint8 array that you can use to patch the image directly. Actually only the alpha map is usefull.


[RGBA, bbox] = __text_to_pixels__ ("Hello", "FreeSerif", 50);
alpha = double (flipud (permute (RGBA(4,:,:), [3 2 1]))) / 255;

anchor_xy = [2, 40];
bbox = [anchor_xy bbox(3:4)];
idx_row =  bbox(2):(bbox(2)+bbox(4)-1);
idx_col =  bbox(1):(bbox(1)+bbox(3)-1);

fname = "/tmp/Capture.png"
im = im2double (imread (fname));
ncolor = 1;
if (numel (size (im)) == 3)
  ncolor = 3;
endif

fontcolor = [1 0 0];
for ii = 1:ncolor
  color = fontcolor(ii);
  im (idx_row, idx_col,ii) = im (idx_row, idx_col,ii) .* (1-alpha) + ...
                             color * ones (bbox(4),bbox(3)) .* alpha;
endfor
imshow (im, "colormap", copper ());



(file #50405)

Pantxo Diribarne <pantxo>
Group Member
Fri 04 Dec 2020 12:48:02 PM UTC, comment #8: 

This will always be a slow workaround, no? Is there a way to obtain a raster image from a string and font, with a call to FreeType or Qt
drawText like in https://stackoverflow.com/questions/22186544/ ?

Guillaume <gyom>
Fri 04 Dec 2020 12:04:43 PM UTC, comment #7: 

@Avinoam: If you're happy with the screen resolution you can use "getframe (gca)" and then "imwrite". Make the figure aspect ratio identical to that of your image, fill the figure and then getframe/print:


## Make the axes fill the figure
hax = axes ("position", [0 0 1 1]);

imshow ("default.img");

## Scale the figure to match the image aspect ratio
pos = get (gcf, "position");
pba = get (gca, "plotboxaspectratio");
pos(4) = pos(3)*pba(2)/pba(1);

set (gcf, "position", pos, "paperpositionmode", "auto")

im = getframe (gca);
imwrite (im.cdata, "/tmp/toto_getframe.png")
print -r300 /tmp/toto_print.png


When using "print", strangely, I still get empty pixels on the right and at the bottom.

Pantxo Diribarne <pantxo>
Group Member
Fri 04 Dec 2020 09:25:12 AM UTC, comment #6: 

I have tried to implement inserText, using the code in comment #1.
The problem is that the image is padded with white margins.

Attached the m files, input image (cameraman.tif) and output image (im_txt.png)

How do I create an output image without the white margins?

Thanks.



(file #50397, file #50399, file #50400)

Avinoam Kalma <avinoam>
Group Member
Mon 07 Oct 2019 11:53:35 AM UTC, comment #5: 

I guess the function would go into the non-existing computer vision package. As discussed before, it's fine to use the image processing package until there's enough of these to create a package of its own.

Rik wrote on comment #2:

> On the other hand, servers built without any graphic capabilities are usually just for doing computes as quickly as possible.  Interacting with images typically requires a bit of human interaction.  You might mant to insert text, and then display the resulting image to see whether the results are pleasing.  So a machine that one would use to do image processing probably has graphic capabilities built in.


It's not uncommon for those computers to annotate the results with some text. It doesn't need to be pretty, you may just want to write a count, confidence, or threshold value in the corner or the figure, so no need for human interaction.

Still, insertText is about annotating a figure, so it's reasonable to expect that octave will be built if plotting enabled.

Carnë Draug <carandraug>
Group Member
Sun 06 Oct 2019 06:59:05 PM UTC, comment #4: 

adding people to cc

Avinoam Kalma <avinoam>
Group Member
Mon 30 Sep 2019 02:49:41 PM UTC, comment #3: 

It will work on all platforms because plotting is supported on all platforms, but if you have configured Octave not to have any sort of plotting (No Qt, FLTK, gnuplot) then it would fail.

On the other hand, servers built without any graphic capabilities are usually just for doing computes as quickly as possible.  Interacting with images typically requires a bit of human interaction.  You might mant to insert text, and then display the resulting image to see whether the results are pleasing.  So a machine that one would use to do image processing probably has graphic capabilities built in.

It doesn't strike me as a problem to require users who want to use this particular function to have a fully functional version of Octave.

Rik <rik5>
Group administrator
Mon 30 Sep 2019 09:14:07 AM UTC, comment #2: 

Thanks for the feedback. What I was wondering was whether your suggestion would always work on all platforms and even without any graphics backend (for invisible figure and print)?

Guillaume <gyom>
Fri 27 Sep 2019 05:25:31 PM UTC, comment #1: 

Changing the category to Octave Forge Package since any implementation would take place in a package, rather than in octave-core.

The function can easily be simulated by creating a figure, puting the image in it, putting the text in it, and then pulling the pixels back out with print.  A rough draft is shown below.


function RGB = insertText (img, pos, txt)
  hf = figure ("visible", "off");  # Invisible figure for combining images
  imshow (I);                      # Add image
  text (pos(:,1), pos(:,2), txt);  # Add text
  RGB = print (hf, "-RGBImage");   # Extract new image
  close (hf);                      # cleanup temporary figure
endfunction


This works for me with a simple invocation like


hf = figure;
sombrero
img1 = print ("-RGBImage");
clf;
imshow (img1);
img2 = insertText (img1, [200, 200], "Hello World");
hf2 = figure;
imshow (img2)



Rik <rik5>
Group administrator
Fri 27 Sep 2019 04:18:11 PM UTC, original submission:  

Matlab seems to have a function in its Computer Vision toolbox to directly insert text in an image (without any display):

https://www.mathworks.com/help/vision/ref/inserttext.html

Would there be a way to implement this by reusing an existing low-level Octave functionality dealing with fonts (in Qt?)?

Guillaume <gyom>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #50412:  insertText.m added by gyom (9KiB - text/x-objcsrc)
file #50405:  __text_to_pixels__.cc added by pantxo (2KiB - text/x-c++src)
file #50397:  cameraman.tif added by avinoam (64KiB - image/tiff)
file #50398:  im_txt.png added by avinoam (59KiB - image/png)
file #50399:  insertText.m added by avinoam (352B - application/octet-stream)
file #50400:  check_insertText.m added by avinoam (105B - application/octet-stream)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by siko1056 (Updated the item)
  • -email is unavailable- added by pantxo (Posted a comment)
  • -email is unavailable- added by avinoam (Posted a comment)
  • -email is unavailable- added by avinoam
  • -email is unavailable- added by avinoam
  • -email is unavailable- added by gyom (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 16 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-04-28 siko1056 Summary[Octave-Forge] (image) Implementation of insertText [octave forge] (image) Implementation of insertText
    2020-12-07 gyom Attached File- Added insertText.m, #50412
    2020-12-06 avinoam StatusConfirmed In Progress
        Releasedev other
    2020-12-04 pantxo Attached File- Added _text_to_pixels_.cc, #50405
    2020-12-04 rik5 Carbon-CopyRemoved 72865 -
    2020-12-04 avinoam Attached File- Added cameraman.tif, #50397
        Attached File- Added im_txt.png, #50398
        Attached File- Added insertText.m, #50399
        Attached File- Added check_insertText.m, #50400
    2019-10-06 avinoam Summary[Octave-Forge] Implementation of insertText [Octave-Forge] (image) Implementation of insertText
        Carbon-Copy- Added carandraug
        Carbon-Copy- Added hardy
    2019-09-27 rik5 CategoryOctave Function Octave Package
        StatusNone Confirmed
        SummaryImplementation of insertText [Octave-Forge] Implementation of insertText

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code