Here is the path "M276,189h268c5.5,0,10,4.5,10,10v196c0,5.5-4.5,10-10,10H276 c-5.5,0-10-4.5-10-10V199C266,193.5,270.5,189,276,189z"
This is the input image:
After applying the path to the image by using the below code
draw = Magick::Draw.new
draw.fill 'red'
draw.path path
draw.clip_rule("evenodd")
draw.fill_rule("evenodd")
draw.fill_opacity(0)
draw.draw image
img.trim!
img.write('output.jpg')
This is the output image:
Now I want to cut the red color part of the image. This means expecting the brown color part only visible.
I used the normal image-level crop method. with this Am able to extract the red color part only
But I want to extract the image other than the red color from the output image.
Here is the sample output for the black image
sample output for the black color
Maybe we have to reverse clip or reverse crop to get this ...
The method
img.paint_transparent
will make that red color part as transparent, but we have to pass the color as an argument to the above method. Initially, I tried by giving the color value as red. so it's not worked. Now am reading the one pixel from the image like
redPixel= img.get_pixels(300, 200, 1, 1)[0]
and getting the color from that pixel-like redPixel.to_color and passing the value to the above method ...
we have to set fuzz value to the image
img = Magick::Image.read("diecut.jpg").first
redPixel= img.get_pixels(300, 200, 1, 1)[0]
img.fuzz = '25%'
puts redPixel.to_color
newimage=img.paint_transparent(redPixel.to_color)
newimage.write("outPut.png")
newimage.display
Related
I have an image which is in gray scale.
I wanted to upsamle the image, so I used the following code,
img = cv2.imread('unnamed.jpg')
img_1 = scipy.ndimage.zoom(img,3, order=1)
print(img.shape, img_1.shape)
and the output is
(187, 250, 3) (561, 750, 9)
For some reason, I cannot use plt.imshow(img_1) as it gives error,
TypeError: Invalid shape (561, 750, 9) for image data
I'd appreciate it if somebody could help me with it.
It looks like your image has 3 channels, which means it is not in grayscale. So, either convert it to grayscale first, and apply zoom, or, in case you want to keep the image in color mode, don't apply zoom on the image channels, because it does not make much sense.
# 1st option returns grayscale image
img = cv2.imread('unnamed.jpg',0) # returns grayscale image
img_1 = scipy.ndimage.zoom(img,3, order=1)
# 2nd option returns BGR image
img = cv2.imread('unnamed.jpg',1) # returns RGB image
img_1 = scipy.ndimage.zoom(img,[3,3,1], order=1) # zoom should contain one value for each axis.
I'm writing a code that should detect frames in a video that have colored lines. I'm new to openCV and would like to know if I should evaluate saturation, entropy, RBG intensity, etc. The lines, as shown in the pictures, come in every color and density. When black and white, but they are all the same color inside a given frame. Any advice?
Regular frame:
Example 1:
Example 2:
You can use something like this to get the mean Saturation and see that it is lower for your greyscale image and higher for your colour ones:
#!/usr/bin/env python3
import cv2
# Open image
im =cv2.imread('a.png',cv2.IMREAD_UNCHANGED)
# Convert to HSV
hsv=cv2.cvtColor(im,cv2.COLOR_BGR2HSV)
# Get mean Saturation - I use index "1" because Hue is index "0" and Value is index "2"
meanSat = hsv[...,1].mean()
Results
first image (greyish): meanSat = 78
second image (blueish): meanSat = 162
third image (redish): meanSat = 151
If it is time-critical, I guess you could just calculate for a small extracted patch since the red/blue lines are all over the image anyway.
I have 26 PNG files, each with an image of a letter of the alphabet. They've all been fully cropped to the letter shape with the result that when I insert them into an image, letters with tails all 'sit on the line'
Each letter is in black, with a transparent background. Each PNG has different dimensions, because of the differing letter shapes
I thought I'd remediate this by adding a transparent border of a different size depending on the source file, to make common datum for all the letters, so that 'a' for example would have some transparent space at the bottom.
I've coded up the calculcation for each letter, but I have two issues:
1) Even before applying the operation, I can't seem to read the file in and write it to a new unchanged file in OpenCV. The transparency in the image is replaced with black.
2) While I can add a colour border, I can't seem to add a transparent border.
Original Image:
Read in, and written out:
Apparenly with a blue border, but maximum transparancy:
I have a feeling that if I can sort out the first problem, the second might fall in line. Here is my code:
img = cv2.imread(file)
img_with_border = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=[-255,0,0,255])
#img_with_border = img
cv2.imwrite(newfile, img_with_border, [cv2.IMWRITE_JPEG_QUALITY, 100])
I'd appreciate some help on what is going on here with transparancy. Is OpenCV the right tool to use?
Thanks,
Jeff.
To load a PNG image with 4 channels in OpenCV, use im = cv2.imread(file, cv2.IMREAD_UNCHANGED). You will obtain a BGRA image.
To change the alpha value, you have to change the fourth channel of the image. This means that to create your transparent border you have to pass a value (B, G, R, 0) and not [-255, 0, 0, 255]. (What is that -255 by the way ?). B, G and R can be 0, it doesn't matter.
Also, make sure you write to a PNG image to keep the transparency. You seem to be writing your result as JPEG.
what I want to do is change only the black color of my original image to another color, and keep for example white color of my image
what I have tried is:
cell.iconeEcoute?.image = cell.iconeEcoute?.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
cell.iconeEcoute?.tintColor = colorHexIcone
But this code change the entire image color, and I want to keep green color and replace only black color in the image
any help would be appreciated
You can achieve it by taking separate View. Then give different color and alpha as you want. and you will see that part in different color.
I'm trying to draw gradient lines on an image. I want my lines to be green colored and I use Scalar(0,255,0). Still, I'm getting only black color. For Scalar(0,0,0) also I'm getting black. For Scalar(255,255,255) I get white, but no other color for any combination. Part of the code is given below:
line(visual_image,
Point(x1*scaleFactor, y1*scaleFactor),
Point(x2*scaleFactor, y2*scaleFactor),
Scalar(0,255,0),
1,8,0);
since you can't draw coloured lines,circles,etc into a grayscale image, you have to convert it to 3 channels first :
Mat rgb;
cvtColor(visual_image, rgb, CV_GRAY2BGR);
// now draw your lines:
line( rgb,
Point(x1*scaleFactor, y1*scaleFactor),
Point(x2*scaleFactor, y2*scaleFactor),
Scalar(0,255,0),
1,8,0);
Just change to new Scalar(100,100, 0,255), .It need 4 parameter for R,G,B, Alpha.
Ref link: https://www.programcreek.com/java-api-examples/?class=org.opencv.imgproc.Imgproc&method=circle
https://stackoverflow.com/a/44352231/7408574