Possible to import OpenCV in grasshopper python for image processing?

First, Happy New Year to all guys!
Now I am learning OpenCV-python for image processing.
I wonder if I can import OpenCV in grasshopper python (I installed OpenCV and can import it in python; however, I can’t find it in the grasshopper python).
If yes, so appreciated that I could have some clues.
Thanks again!

Hi,

probably yes, but in Rhino 7 through Hops: Create CPython components using Hops in Grasshopper

1 Like

Thank you so much, @w.radaczynski!
Let me chek the link first then I will come back.

Hi, I would recommend trying EmguCV instead

Thank you so much, @pavlom!
I will try it today and come back.

Hi @w.radaczynski , today I followed the method in the link you provided. It really works!!
Hops is an amazing tool.
Thank you so much for providing such a valuable clue.
And I hope everyone can have a look at Hops, even you will not use it.

2 Likes

Great to hear it works! It will be also great if you could show something you did with combination of GH and OpenCV :wink: I’d love to see where such combination of tools can be useful :slight_smile:

Hi @w.radaczynski,
what I did is a very simple test.
Just import an image in Hops, then cut it, and draw the contour in Hops with OpenCV.
Now I can just export the contour length to Grasshopper, as pic shows


However, I don’t know how to export the resulted image into Grasshopper. I searched a lot, but still can’t figure out. Do you have some clues?
Thanks again!

Have you tried Squid’s plugin component called Quick Preview? :

It takes Bitmap as input and display it in GH. There is also this built-in Import Image, but it displays image in Rhino Viewport then:

Do you need something like this or something else?

Hi @pavlom, I downloaded the lastest Engu CV and followed the steps.
However, it seems the new version changes a lot and the steps are not working.
I would like to pause for a while in this direction.
Thanks again!

Hi @w.radaczynski, thank you for your kind and prompt reply!
I know these two GH components, however, I don’t know how to export the resulted image to them.
Here is my Hogs code to define the hops component:
@hops.component(

"/img1",    # link name

name="img1",    #

description="import image and cut and show",

inputs=[    # imput parameters

    hs.HopsNumber("x1", "x1", "x range"),

    hs.HopsNumber("x2", "x2", "x range"),

    hs.HopsNumber("y1", "y1", "x range"),

    hs.HopsNumber("y2", "y2", "x range"),

   

],

outputs=[hs.HopsNumber("img", "conlength", "finaimg")

         

],

)

What should I write in outputs= if I want to export an image variable called “img_cut” for example?
I tried to use HopsString, however, it exports the name “img_cut” instead of the image file.
Thank you!

Looking at this comment: Create CPython components using Hops in Grasshopper - #36 by seghierkhaled

it looks like the idea was to save the image/bitmap on your computer using Python, and then pass e.g. only path to this file, so other component will read the path and display it.

Hi @w.radaczynski, yes, your clue works well again!
I saved the image on computer in Hops, then read it using quick review. (Only problem is that once it is shown, it can’t be changed with variable changes. I need to think about it.)


Thank you so much!

1 Like

What is the image showing? Looks interesting :wink:

Try maybe to name file that you save differently everytime you save it, for example you can create a time stamp with Python (Get current timestamp using Python - GeeksforGeeks) and add it to a name of the file. That will update the path every time which should as well update preview everytime. The drawback would be that every time you change the variable then new image is created which leads to multiple images in the directory. Maybe there is some better option :wink:

1 Like

Hi @w.radaczynski,
Thanks for your suggestion. I tried it and it works! However, to delete so many pics after one change gives me some headache :).
Finally I used the Import Image to resolve the problem, since it can synchronize the new image file automatically.
By the way, answer your question: this is an optical coherence tomograph (OCT) image of human vessel.

I am so happy the door of Hops has openned to me and I am sure there will be huge exiting things for me to discover…
Wish more helps form you and all forum guys in the future!

3 Likes

Looks really interesting! Would love to see more results in future :wink: