I can't include a color code in my graph using visreg - visreg

I'm relatively new to R, and I'm using the visreg package to plot a model. I can't figure out how to make that the points of the graph include a color of one of the considered factors ("Site").
enter image description here
My code is:
print(visreg(modeloSeleccionado, xvar = "Season", ylab = varList[k] , by = "Site", gg=TRUE, overlay=FALSE, points.par=list(size=2, alpha=1), jitter=TRUE))
Thanks!

Related

How does osmnx.plot_graph determine which edges get which colors?

I've followed this tutorial:
https://towardsdatascience.com/creating-beautiful-maps-with-python-6e1aae54c55c
and the one this above was derived from.
They pass a list of edge colors to the plot_graph function
like so:
fig, ax = ox.plot_graph(gdf, node_size=0, bbox = (north, south, east, west),figsize=(height, width),
dpi = 96,bgcolor = bgcolor,
save = False, edge_color=roadColors,
edge_linewidth=roadWidths, edge_alpha=1)
I don't think they're assigned the way that the tutorial indicates.
On the github I found get_edge_colors_by_attr which seems to take attributes into account.
How are the colors assigned?
Specifically I am asking because I'd like to plot "highways" in different colors based on their openstreetmap tag.
How does osmnx.plot_graph determine which edges get which colors?
You can see how it does it here. Essentially, it either applies a single color to all edges or, if you passed it a list of colors, it assigns the first color in the list to the first edge in the graph, the second to the second, the third to the third, and so on.
Specifically I am asking because I'd like to plot "highways" in different colors based on their openstreetmap tag.
You can create a list of colors based on the edges' highway attribute values:
import osmnx as ox
G = ox.graph_from_place('Piedmont, California, USA', network_type='drive')
# assign colors to edges based on "highway" value
hwy_color = {'residential': 'gray',
'secondary': 'r',
'tertiary': 'y',
'tertiary_link': 'b',
'unclassified': 'm'}
edges = ox.graph_to_gdfs(G, nodes=False)['highway']
ec = edges.replace(hwy_color)
# plot graph using these colors
fig, ax = ox.plot_graph(G, edge_color=ec)
Also, you mentioned get_edge_colors_by_attr but note that per the docs the attribute must be numeric.

How to matched object from main image with the template sub-Image using normxcorr2

I try to determine the coordinates of a puzzle piece on the original image using the normxcorr2 function. Then I draw a rectangle on the correspondence of the two elements. Unfortunately, I notice that the coordinates that this match has given me are not good. Could someone have an idea how to improve the use of this feature and get some better results.
The puzzle piece has the name "cpiece" and the original picture has the name "bild"
clear all;
close all;
clc
cpiece = im2gray(imread('cpiece1.jpg'));
bild = im2gray(imread('original.jpg'));
figure(1)
montage({bild,cpiece})
c = normxcorr2(cpiece,bild);
figure(2)
surf(c)
shading flat
[ypeak,xpeak] = find(c==max(c(:)));
yoffSet = ypeak-size(cpiece,1);
xoffSet = xpeak-size(cpiece,2);
figure(3)
imshow(bild)
drawrectangle(gca,'Position',[xoffSet,yoffSet,size(cpiece,2),size(cpiece,1)],'FaceAlpha',0);
It seems, the problem returns back to the quality of your template image, And Check if the scales between original image and the template are exactly the same

Rice grain Segmentation in Matlab

I have an image of multiple rice grains in jpg format. I need to separate the individual rice grains into a separate images.
The input image is as below:
The resultant images should contain individual rice as below:
Currently I am using the following code:
close all;
BW = imread('img11_Inp','jpg');
L = bwlabel(BW)
figure
imshow(BW);
CC = bwconncomp(L);
stats = regionprops(L,'Image');
stats
%Display the first component as an image
Image1 = stats(2).Image
figure
imshow(Image1);
There are two problems with this code. Firstly, it is showing only two images rice grains in the "stats" structure and secondly it is also showing some noise. Please let me know what is the problem with this code
Look at this code with the comments
close all;
clear
I = imread('rice.jpg'); % jpg add noise. TODO: use png format
level = graythresh(I); % Global image threshold using Otsu's method
BW1 = imbinarize(I,level); % need to threshold, jpg add low level noise.
BW2 = imfill(BW1,'holes'); %fill holes inside grains
[L,n] = bwlabel(BW2); % label connected components
B = labeloverlay(I,L);
figure('Name',sprintf('NLables=%d',n)); imshow(B);
stats = regionprops(L,{'Centroid','Area'});
for i=1:length(stats)
x=stats.Centroid(i,1);
y=stats.Centroid(i,2);
area=stats.Area(i);
text(x,y,sprintf('Area=%d',area),'FontSize',8);
end

ColorThief returning wrong dominant color

In my UWP app we have images that users upload and they can be pretty much any size - so when I load these into in an Image control I resize them proportionally so they fit best into our image container (which is fixed at 112w x 152h) but as some images are wider than higher what I want to is find the background color of the image so I can set the Background of the image.
So to do this I am using the NuGet ColorThief package and the code below (taken from the sample I found there) is returning me the wrong dominant color in many cases:
var ct = new ColorThief();
QuantizedColor qc = await ct.GetColor(decoder); //BitmapDecoder containing image
Windows.UI.Color c = Windows.UI.Color.FromArgb(qc.Color.A, qc.Color.R, qc.Color.G, qc.Color.B);
setImageBackgroundColor(bgColour);
One image I tried it on was this one - now the dominant colour should clearly be black but it's returning some medium grey colour.... so is there something wrong with my code or my image or is there something else I need to do? I'm just not finding it reliable but I know others use it without issue.
According to the source code of the library, it seems it is actually averaging out the colors from the color palette of the picture
public async Task<QuantizedColor> GetColor(BitmapDecoder sourceImage, int quality = DefaultQuality,
bool ignoreWhite = DefaultIgnoreWhite)
{
var palette = await GetPalette(sourceImage, 3, quality, ignoreWhite);
var dominantColor = new QuantizedColor(new Color
{
A = Convert.ToByte(palette.Average(a => a.Color.A)),
R = Convert.ToByte(palette.Average(a => a.Color.R)),
G = Convert.ToByte(palette.Average(a => a.Color.G)),
B = Convert.ToByte(palette.Average(a => a.Color.B))
}, Convert.ToInt32(palette.Average(a => a.Population)));
return dominantColor;
}
In this particular case, I think setting ignoreWhite to true might return the actual black color, although it seems true is indeed the default setting.
I recommend using the other public method the library provides: GetPalette, to see what is the actual color palette of this image. This should explain why you are getting this particular color.
var palette = await GetPalette(decoder);
In my opinion you need to set quality from 5 to 10 and use GetPalette method with first color from palette.
QuantizedColor dominantColor = colorThief.GetPalette(tempBitmap, 5, 5, true)[0];

segment object(leaf) which is on the white paper using image processing

I want to get only leaf from an image.
The background is a normal white paper(A4) and there is some shadow.
I apply some method (structure element,edge detection using filter) but I cannot find the general way which can apply all the image.
these are examples.
Are there better methods for this problem??
thank you
another example.
and the result I got is
By using
hsv_I = rgb2hsv(I);
Is = hsv_I(:,:,2);
Is_d = imdilate(Is,strel('diamond',4));
Is_e = imerode(Is,strel('diamond',2));
Is_de = imerode(Is_d,strel('disk',2));
Is_def = imfill(Is_de,'holes');
Is_defe = imerode(Is_def,strel('disk',5));
Then Is_defe is a mask to segment
But the method that i did is very specific. I cannot use this in general.
If you have the Image Processing Toolbox, you could do as follows:
The code below first estimates the threshold with the function graythresh, thresholds the image and fills holes with the imfill function. Suppose I is a cell containing your RGB images:
for k=1:length(I)
t=graythresh(rgb2gray(I{k}));
BW{k}=imfill(~im2bw(I{k}, t), 'holes');
subplot(length(I),1,k), imshow(BW{k});
end

Resources