Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color. Obj file #2

Open
cwo75 opened this issue Feb 12, 2021 · 5 comments
Open

Color. Obj file #2

cwo75 opened this issue Feb 12, 2021 · 5 comments

Comments

@cwo75
Copy link

cwo75 commented Feb 12, 2021

Thank you for the quick andere Titel my
Demo_obj question!!!!!!!!
I have 512x512x3 RGB values for my surface but when in put it to write_obj_color
I get an error. ( if you need more info I send you the source
Thank you
Christian

@JBKacerovsky
Copy link
Owner

Hi christian, from the information you gave it sounds like you are applying uv coordinates and a texture file. Is that correct? While I think that would be a more elegant solution in line with .obj best practices that is not how the current version of the function is built.
I tried to make the script similar in terms of input and usage to the built in patch function. So the color input requires either a vector of a continuous variable, Nx1 matrix and a colormap (one of the built in Matlab colormaps) or a list of RGB values, Nx3 matrix, where N should be equal to the number of faces or vertices of the mesh object. Each unique colour is saved as a different material. I know this is not super efficient, since it can lead to a potentially very large number of materials on the same object (and it doesn't really allow for smooth colours), but this was most straightforward to implement as a direct adaptation from the patch function, which I was using in Matlab before.

Does this answer your question? If you want to use this function, I think it should be relatively straightforward to pull out the list (Nx3 matrix) of RGB values from the colormap file you seem to have using uv coordinates. If you still run into issues (or if I misunderstood the question) please provide some example code and data.

Benjamin

@cwo75
Copy link
Author

cwo75 commented Feb 14, 2021 via email

@JBKacerovsky
Copy link
Owner

Hi Christian,

Oh cool, ich komm' auch aus Österreich :) I studied in Vienna.

I took a look at your example. The error you are getting seems to be because different numeric types are passed to linspace. fvc.facevertexcdata from surf2patch on an 8-bit png is of numeric type uint8, but the other variables passed to linspace in line 130 are of type double, so it throws an error.

You can simply convert your color vector to double and it should fix the issue. I also added a quick fix into the code (simple enforcing types double for the relevant variables) so if you pull the repo it should be fixed and you should be able to run the obj_write_color.m function without having to convert anything.

Please note that the function is written specifically for triangle meshes, so you will have to specify 'triangles' for surf2patch (see code example below). If you pass quad-meshes in, some bizarre shapes emerge :p

below you have a code example (using one of the .png images that come with the Matlab distribution I believe) that shows the fix with the 'old' and new versions of the function:

`A=imread('peppers.png');
B=A(:, :, 1);
[X, Y] = meshgrid(1:512, 1:384); %% X, Y as pixel coordinates

Z=rescale(mean(A, 3), 0, 90); %% Z scale as averagee rgb values

fvc=surf2patch(X,Y,Z,B, 'triangles');

% test with matlabb figure
figure
patch(fvc, 'edgecolor', 'none', 'facevertexcdata', double(fvc.facevertexcdata), 'facecolor', 'flat');
colormap jet
axis equal

%% export colour obj file
% this fixes the issue of the previous iteration of the function
% the numeric typee of 'colors' gets passed to cmin and cmax. The linspace
% function requires all input variables to be of the same type, in this
% case double.
obj_write_color((fvc), 'demo', double(fvc.facevertexcdata), 'colorMap', 'jet', 'illum', 10);

%% with updated version (feb-15) of obj_write_color.m
% the most recent update of obj_write_color.m converts cmin and cmax to
% double, so it should also work without issue if the numeric type of
% colors is 'uint8', as is the case in the above example.
obj_write_color((fvc), 'demo_double', (fvc.facevertexcdata), 'colorMap', 'jet', 'illum', 10);

%% testing accessing the rgb values for each pixel directly from source file and mapping to mesh/.obj file
yy = fvc.vertices(:, 1);
xx = fvc.vertices(:, 2);

r = A(:, :, 1);
r = r(sub2ind(size(r), xx, yy));
g = A(:, :, 2);
g = g(sub2ind(size(g), xx, yy));
b = A(:, :, 3);
b = b(sub2ind(size(b), xx, yy));

rgb = [r, g, b];

rgb = double(rgb)/255;

obj_write_color((fvc), 'demo_rgb', rgb, 'illum', 5);
`

@cwo75
Copy link
Author

cwo75 commented Feb 16, 2021 via email

@cwo75
Copy link
Author

cwo75 commented Feb 16, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants