我试图直接读取图像为黑色和白色.
我最近将我的OpenCv版本更新为3.0.0-dev,我之前使用的代码不再起作用.
img = cv2.imread(f, cv2.CV_LOAD_IMAGE_GRAYSCALE)
Run Code Online (Sandbox Code Playgroud)
适用于2.4,但不适用于新版本,因为没有字段CV_LOAD_IMAGE_GRAYSCALE.
有什么建议?
注意:我知道这cv2.imread(f,0)会有效,但我不喜欢在我的代码中使用未命名的常量.谢谢!
嗨伙计们,我需要你的帮助,我正在尝试使用红色,绿色,蓝色的平均值将彩色图像转换为灰度.但它出现了错误,
这是我的代码
imgWidth = myBitmap.getWidth();
imgHeight = myBitmap.getHeight();
for(int i =0;i<imgWidth;i++) {
for(int j=0;j<imgHeight;j++) {
int s = myBitmap.getPixel(i, j)/3;
myBitmap.setPixel(i, j, s);
}
}
ImageView img = (ImageView)findViewById(R.id.image1);
img.setImageBitmap(myBitmap);
Run Code Online (Sandbox Code Playgroud)
但是当我在模拟器上运行我的应用程序时,它会强制关闭.任何的想法?
我已经用以下代码解决了我的问题:
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get one pixel color
pixel = src.getPixel(x, y);
// retrieve color of all channels
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
// take conversion …Run Code Online (Sandbox Code Playgroud) 有没有办法让灰度图像过滤器在没有JavaScript或SVG的情况下在IE 10上运行?
我一直在IE 10以外的所有浏览器中成功使用以下代码.
img{
filter:url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\'><feColorMatrix type=\'matrix\' values=\'0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\'/></filter></svg>#grayscale"); /* Firefox 10+, Firefox on Android */
filter:gray; /* IE6-9 */
-webkit-filter:grayscale(100%); /* Chrome 19+, Safari 6+, Safari 6+ iOS */
-moz-filter: grayscale(100%);
-o-filter: grayscale(100%);}
Run Code Online (Sandbox Code Playgroud) 如何将灰度值(0-255)转换为RGB值/表示?它适用于SVG图像,它似乎没有灰度支持,只有RGB ......
注意:这不是RGB - >灰度,已在另一个问题中回答,例如将RGB转换为灰度/强度)
更新
我刚刚意识到去饱和只适用于Chrome.如何在FF,IE和其他浏览器中使用它?(标题改变了)
我按照这里的建议将彩色图片 转换为灰度:在HTML/CSS中将图像转换为灰度
它的效果很好(在Chrome中):http://jsfiddle.net/7mNEC/
<img src="https://imagizer.imageshack.us/v2/350x496q90/822/z7ds.jpg" />
// CSSS
img {
filter: url(~"data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\'><feColorMatrix type=\'matrix\' values=\'0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\'/></filter></svg>#grayscale");
-webkit-filter: grayscale(100%);
-moz-filter: grayscale(100%);
-ms-filter: grayscale(100%);
-o-filter: grayscale(100%);
filter: gray;
}
img:hover {
filter: none;
cursor: pointer;
}
Run Code Online (Sandbox Code Playgroud)
但我无法消除鼠标上的去饱和现象.
对我做错了什么想法?
我有这个扩展(找到obj-c并将其转换为Swift3)以获得相同UIImage但灰度:
public func getGrayScale() -> UIImage
{
let imgRect = CGRect(x: 0, y: 0, width: width, height: height)
let colorSpace = CGColorSpaceCreateDeviceGray()
let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue).rawValue)
context?.draw(self.cgImage!, in: imgRect)
let imageRef = context!.makeImage()
let newImg = UIImage(cgImage: imageRef!)
return newImg
}
Run Code Online (Sandbox Code Playgroud)
我可以看到灰色图像,但它的质量非常糟糕......我唯一能看到的与质量相关的是bitsPerComponent: 8在上下文构造函数中.然而,看看Apple的文档,这是我得到的:
它表明iOS只支持8bpc ......那么为什么我不能提高质量呢?
我正在尝试将此PDF转换为灰度:https://dl.dropboxusercontent.com/u/10351891/page-27.pdf
Ghostscript(v 9.10)与pdfwrite设备失败,"无法将色彩空间转换为灰色,将策略还原为LeaveColorUnchanged".信息.
我能够通过中间ps文件(使用gs,pdftops(v 0.24.3)或pdf2ps)转换它,但这个转换光栅化整个PDF.我尝试了很多其他的东西:使用qpdf(v 5.0.1)或pdftk(v 1.44)规范化PDF,将其转换为svg文件并通过Inkscape(v 0.48.4)转换回PDF ...似乎没什么上班.
我找到的唯一一个解决方案(在生产环境中不适合我)是在我的Mac上使用预览并手动或使用Automator脚本应用Quartz Gray Tone滤镜.
有人找到另一种工作方式吗?或者是否可以规范化PDF或修复问题以防止Ghostscript消息"无法转换颜色空间..."或以另一种方式强制颜色空间?
谢谢!
我在从灰度转换为彩色时遇到了麻烦,它在chrome中工作,但就是这样.
这是HTML:
<div id="post" style="background-image:url('bg.png');background-repeat:no-repeat;">
<p><a href="/post.php?id=1">Title - Date</a></p>
</div>
Run Code Online (Sandbox Code Playgroud)
这是CSS:
#post{
padding:0;
margin:0 auto;
margin-bottom:25px;
border:solid 1px #000;
height:150px;
width:750px;
display:block;
filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\'><feColorMatrix type=\'saturate\' values=\'0.5\'/></filter></svg>#grayscale");
filter: gray alpha(opacity=50);
-webkit-filter: grayscale(50%);
-webkit-transition: 0.3s all ease;
-o-transition: 0.3s all ease;
-moz-transition: 0.3s all ease;
transition: 0.3s all ease;
-webkit-backface-visibility: hidden;
}
#post:hover{
filter: none;
-webkit-filter: grayscale(0%);
}
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助,非常感谢.
我想打开两个png图像文件并将它们并排显示以进行视觉比较.我有这个代码用于打开一个png文件(我从stackoverflow.com上的unutbu获得):
import numpy as np
import pylab
import matplotlib.cm as cm
import Image
fname='file.png'
image=Image.open(fname).convert("L")
arr=np.asarray(image)
pylab.imshow(arr,cmap=cm.Greys_r)
pylab.title('title')
pylab.show()
Run Code Online (Sandbox Code Playgroud)
有没有办法修改此代码以打开并显示2个png文件与他们自己的标题并排?
我正在尝试使用imagemagick命令行工具将灰度图像转换为RGB .
它适用于PNG图像,使用:
convert image.png -define png:color-type=2 result.png
Run Code Online (Sandbox Code Playgroud)
(摘自"如何使用image magick将灰度png图像从命令行转换为RGB")
虽然检查identify -format %r result.png仍然会返回DirectClass灰色,我可以看到它使用,gdalinfo因为现在列出了3个频段/频道:
gdalinfo [成功转换PNG]:
Driver: PNG/Portable Network Graphics
Files: result.png
Size is 567, 479
Coordinate System is `'
Image Structure Metadata:
INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 479.0)
Upper Right ( 567.0, 0.0)
Lower Right ( 567.0, 479.0)
Center ( 283.5, 239.5)
Band 1 Block=567x1 Type=Byte, ColorInterp=Red
Band 2 Block=567x1 …Run Code Online (Sandbox Code Playgroud) grayscale ×10
css3 ×3
python ×2
rgb ×2
android ×1
colors ×1
command-line ×1
css ×1
filter ×1
firefox ×1
ghostscript ×1
html5 ×1
image ×1
imagemagick ×1
jpeg ×1
matplotlib ×1
opencv ×1
pdf ×1
png ×1
swift ×1
transition ×1
uiimage ×1