Convert RGB image to 1 channel image (black/white)
If I understand your question, you want to apply a black and white thresholding to the image based on a pixel's luminance. For a fast way of doing this, you could use my open source GPUImage project (supporting back to iOS 4.x) and a couple of the image processing operations it provides.
In particular, the GPUImageLuminanceThresholdFilter and GPUImageAdaptiveThresholdFilter might be what you're looking for here. The former turns a pixel to black or white based on a luminance threshold you set (the default is 50%). The latter takes the local average luminance into account when applying this threshold, which can produce better results for text on pages of a book.
Usage of these filters on a UIImage is fairly simple:
UIImage *inputImage = [UIImage imageNamed:@"book.jpg"];
GPUImageLuminanceThresholdFilter *thresholdFilter = [[GPUImageLuminanceThresholdFilter alloc] init];
UIImage *quickFilteredImage = [thresholdFilter imageByFilteringImage:inputImage];
These can be applied to a live camera feed and photos taken by the camera, as well.
Convert RGB image to black and white
You don't need this:
r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
gray = (0.2989 * r + 0.5870 * g + 0.1140 * b)
because your image is already grayscale, which means R == G == B
, so you may take GREEN channel (or any other if you like) and use it.
And yeah, specify the colormap for matplotlib
:
plt.imshow(im[:,:,1], cmap='gray')
How to Convert RGB images dataset to single channel grayscale?
you directly read images as grayscale with:
im_gray = cv2.imread('gray_image.png', cv2.IMREAD_GRAYSCALE)
or you can convert an rgb image to grayscale with:
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
How can I convert an RGB image into grayscale in Python?
How about doing it with Pillow:
from PIL import Image
img = Image.open('image.png').convert('L')
img.save('greyscale.png')
If an alpha (transparency) channel is present in the input image and should be preserved, use mode LA
:
img = Image.open('image.png').convert('LA')
Using matplotlib and the formula
Y' = 0.2989 R + 0.5870 G + 0.1140 B
you could do:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
img = mpimg.imread('image.png')
gray = rgb2gray(img)
plt.imshow(gray, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.show()
Directly convert black and white image: 3-channel to 1-channel?
Depending on the application, it may be enough to simply feed one of the channels to threshold()
. You can split the 3-channel image using split()
, it saves some time over cvtColor()
as it does not have to do 3 multiplications per pixel.
How fast in Python change 3 channel rgb color image to 1 channel gray?
I had this Problem before.This is the Best way:
Your code is correct but needs some more changes to be suitable for a grayscaled image. Here is the Code:
ii = cv2.imread("0.png")
gray_image = cv2.cvtColor(ii, cv2.COLOR_BGR2GRAY)
print(gray_image)
plt.imshow(gray_image,cmap='Greys')
plt.show()
and this is the result:
[[196 196 197 195 195 194 195 197 196 195 194 194 196 194 196 189 188
195 195 196 197 198 195 194 194 195 193 191]
.
.
.
[194 194 193 193
191 189 193 193 192 193 191 194 193 192 192 191 192 192 193 196 199
198 200 200 200 201 200 199]]
.
OpenCV - I need to insert color image into black and white image and
A colour image needs 3 channels to represent red, green and blue components.
A greyscale image has only a single channel - grey.
You cannot put a 3-channel image in a 1-channel image any more than you can put a 3-pin plug in a single hole socket.
You need to promote your grey image to 3 identical channels first:
background = cv2.cvtColor(background, cv2.COLOR_GRAY2BGR)
Example:
# Make grey background, i.e. 1 channel
background = np.full((200,300), 127, dtype=np.uint8)
# Make small colour overlay, i.e. 3 channels
color = np.random.randint(0, 256, (100,100,3), dtype=np.uint8)
# Upgrade background from grey to BGR, i.e. 1 channel to 3 channels
background = cv2.cvtColor(background, cv2.COLOR_GRAY2BGR)
# Check its new shape
print(background.shape) # prints (200, 300, 3)
# Paste in small coloured image
background[10:110, 180:280] = color
Related Topics
How to Make a Phonegap App for iOS Without Mac
Random Glitchy Rendering of Svg on iOS/Wkwebview
Bootstrap Columns with Flexbox Are Not Taking Proper Width on iOS and Safari
Whats the Correct Way, Using "Init" or "Didmove"
How to Loop Through and Get All The Keys of The Nested Nodes in Firebase
Uitextview - Can't Set Underline or Strikethrough Attributes on Text
How to Use The Coreaudio API in Swift
Resize All Subview in Scrollviewdidzoom
Do Xcode 8 Swift 3 Apps Run on iOS 7 Successfully
How to Detect If My Device Is an iPhone x in Swift 4
Is There an iOS API for Accessing Call Logs
Can You Listen to Firestore Updates When iOS App Is in The Background
Xcode Reference a Framework Instead of Link Binary with Libraries
Xcode6 Goldmaster Submitting Build Failed with Error
Swift: Is Correct to Use Stored Properties as Computed Properties