从图像中删除周围的空白 [英] Remove surrounding whitespace from an image

查看:33
本文介绍了从图像中删除周围的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组我们从客户那里收到的产品图片.每张产品图片都是某物的图片,它是用白色背景拍摄的.我想裁剪图像的所有周围部分,但只将产品留在中间.这可能吗?

I have a block of product images we received from a customer. Each product image is a picture of something and it was taken with a white background. I would like to crop all the surrounding parts of the image but leave only the product in the middle. Is this possible?

例如:[http://www.5dnet.de/media/catalog/product/d/r/dress_shoes_5.jpg][1]

我不想删除所有白色像素,但我确实希望裁剪图像,以便最上面的像素行包含一个非白色像素,最左侧的垂直像素行包含一个非白色像素, 最底部的水平像素行包含一个非白色像素等

I don't want all white pixels removed, however I do want the image cropped so that the top-most row of pixels contains one non-white pixel, the left-most vertical row of pixels contains one non-white pixel, bottom-most horizontal row of pixels contains one non-white pixel, etc.

C# 或 VB.net 中的代码将不胜感激.

Code in C# or VB.net would be appreciated.

推荐答案

我自己编写了代码来执行此操作 - 掌握基础知识并不太难.

I've written code to do this myself - it's not too difficult to get the basics going.

本质上,您需要扫描像素行/列以检查非白色像素并隔离产品图像的边界,然后创建一个仅包含该区域的新位图.

Essentially, you need to scan pixel rows/columns to check for non-white pixels and isolate the bounds of the product image, then create a new bitmap with just that region.

请注意,虽然 Bitmap.GetPixel() 方法有效,但它相对较慢.如果处理时间很重要,则需要使用 Bitmap.LockBits() 将位图锁定在内存中,然后在 unsafe { } 块中使用一些简单的指针直接访问像素.

Note that while the Bitmap.GetPixel() method works, it's relatively slow. If processing time is important, you'll need to use Bitmap.LockBits() to lock the bitmap in memory, and then some simple pointer use inside an unsafe { } block to access the pixels directly.

这篇关于 CodeProject 的文章提供了一些您可能会觉得有用的更多细节.

This article on CodeProject gives some more details that you'll probably find useful.

这篇关于从图像中删除周围的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆