Grasshopper

algorithmic modeling for Rhino

I'm writing a C# script in Grasshopper to perform image averaging.  The goal is to analyze the color gradient of an architectural plan and apply transformations (TBD) to produce different effects.

Although Grasshopper has an image sampler component, I wanted to code one myself but am facing difficulties in assigning a Type Hint to the C# input - there is no Image option. Does anyone have suggestions? Thanks!

Views: 2487

Replies to This Discussion

Here's the Gh file

Attachments:

Could you narrow that down?

If you want to import a bitmap into a C# component (I don't know where you're getting this bitmap instance from though, a different C# component?) you use the system.object hint and then you have to cast the data to the correct type.

I attached a file with two C# components. One which imports an image from a file and resizes it to 50x50 pixels, and another which creates a mesh with coloured vertices.

Attachments:

Thanks! This is quite different from what I had in mind but it is very interesting.

I've updated my Gh and C# script - now am trying to pull the RGB values from each pixel. I'm not sure if the "c.R = Red;" line should be "c.R = double Red;" and output Red? Thanks!

Attachments:

c.R = double Red;

This is not valid C#. If you want to extract the red, green and blue integer values of a colour, you need to write:

int red = c.R;

int green = c.G;

int blue = c.B;

The equals symbol in C# does not encode equality, it encodes assignment. C# doesn't have a concept of defining two values to be equal to each other. Incidental equality can be tested using == though, for example:

if (c.R == 0)

  print("This colour has no red whatsoever.");

Do note that the GetPixel() and SetPixel() methods are quite slow, because they operate on the 'unlocked' bitmap and a lot of error checking happens each time. One way to speed things up is by locking the bitmap and accessing the data in memory directly, but I recommend you use the GH_MemoryBitmap class which does all the hard work for you.

I'll upload an example of this soon.

But first your fixed code. Are you trying to transition from another programming language? Or are you reading C++ tutorials maybe? 

Attachments:

And here's a way to use GH_MemoryBitmap.

Attachments:

Thank you so much! I am learning C# - thus the basic errors. We'll use the corrected script for now and work on applying image transformations. Thanks again!

Anyone knows why the method GetPixel is not available when developing a GH add-on?

I've used the latest rhino visual studio wizard to setup a GH add-on solution and use the assemblies listed below to create a custom component. There are 2 issues:

  1. VS wants to simplify the member access of the "Bitmap" to the "Image" class
  2. GetPixel method is not recognized.

using System;
using System.Collections.Generic;
using Grasshopper;
using Grasshopper.Kernel;
using Rhino.Geometry;
using Grasshopper.Kernel.Types;

...

  var im = System.Drawing.Bitmap.FromFile("some_filename");

  var color = im.GetPixel(1,1);

FromFile returns an Image type. GetPixel is part of the Bitmap type.

There are 3 constructors to create a Bitmap from an Image:

https://msdn.microsoft.com/en-us/library/system.drawing.bitmap(v=vs.110).aspx

There's another one that allows you to create a Bitmap directly from a file.

tnx, i didn't pay attention enough about the return value

In think the two problems might be related. Instead of using var, specifically set the type of im to be a Bitmap. (You may also want to import the System.Drawing namespace)

Bitmap im = new Bitmap("filename");

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service