图像处理与分析

时间:2014-02-04 09:02:37

标签: image-processing

我需要开发一个小程序来完成我可以用photoshop做的事情。那是

1-)选择一张图片 2-)选择你想要的区域 3-)去饱和 4-)应用阈值(123)选择区域

毕竟,我想计算白色区域的百分比。

我没有做任何图像处理项目,所以你喜欢的任何编程语言,框架,库都可以。我想尽快做到这一点。你的建议是什么?

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我认为有两种可能性

ImageMagick

ImageMagick具有Perl abindings,这使得它非常简单,因为您不需要编译器和其他垃圾的负载。查看一些示例here.

ImageMagick也有PHP绑定,所以看看here

但是在你做任何Perl / PHP之前,我会在ImageMagick的命令行中玩游戏。从您的图像开始称为“1.jpg”并在命令行中尝试这些步骤,并在“2.jpg”,“3.jpg”等中查看介入阶段。我怀疑这4行会或多或少地执行您想要的所有操作:

convert 1.jpg -crop 200x400+400+600 2.jpg
convert 2.jpg -colorspace gray -depth 8 3.jpg
convert 3.jpg -threshold 50% 4.jpg
convert 4.jpg -format "%c" histogram:info:
40000:(0,0,0) #000000 (0)
40000:(255,255,255) #FFFFFF (255)

NetPBM是您可以玩的另一个图书馆。

顺便说一句,如果你已经拥有并且熟悉Photoshop,你可以自己在Javascript中编程。您需要使用Adobe ExtendScript Toolkit。在Photoshop安装的“Presets / Scripts”子文件夹中有一些示例 - 它们以“.jsx”结尾。您没有义务使用Javascript,它还可以处理其他几种语言 - VBScript,AppleScript。

概述为here.

文档为here.

以下是我写的一个例子:

// ProcessForWeb - Adobe Photoshop Script
// Description: Copies current image, then flattens it and resizes it to a square shape on a white
// background with a drop shadow. Then saves the copy as an optimised JPEG. Leaves original
// untouched.
// Requirements: Adobe Photoshop CS2, or higher and also you must have a Style saved with the
// name "MyDropShadow"
// Version: 0.1
// Author: Mark Setchell (mark@thesetchells.com)
// Web: http://www.thesetchells.com
// ============================================================================
// Installation:
// 1. Place script in 'C:\Program Files\Adobe\Adobe Photoshop CS#\Presets\Scripts\'
// 2. Restart Photoshop
// 3. Choose File > Scripts > ProcessForWeb
// ============================================================================

// enable double-clicking from Mac Finder or Windows Explorer
// this command only works in Photoshop CS2 and higher
#target photoshop

// bring application forward for double-click events
app.bringToFront();

/*
 TODO:
 3. Improve name of saved file
 */

///////////////////////////////////////////////////////////////////////////////
// ProcessForWeb
///////////////////////////////////////////////////////////////////////////////
function ProcessForWeb() {

    // Pick up active document
    var originalDocument = activeDocument;

     // Create output folder and work out name of JPEG output file
     var ImageName = activeDocument.name;
    ImageName = ImageName.substr(0,ImageName.length-4);
    var OutputFolder = new Folder("~/Desktop/JPEG");
    OutputFolder.create();
    var JPEGname = new File("~/Desktop/JPEG/"+ImageName+".jpg");

    // duplicate original document
    var duplicate = originalDocument.duplicate();

    // flatten the duplicate document and make 8-bit (in case it was 16)
    duplicate.flatten();
    duplicate.bitsPerChannel = BitsPerChannelType.EIGHT;

    // resize the duplicate so that the longest side is IMAGEDIMENSION pixels long
    const IMAGEDIMENSION = 800;
    var w = duplicate.width.value;
    var h = duplicate.height.value;

    if(h>w){
       duplicate.resizeImage(null,IMAGEDIMENSION,72,ResampleMethod.BICUBICSMOOTHER);
    } else {
       duplicate.resizeImage(IMAGEDIMENSION,null,72,ResampleMethod.BICUBICSMOOTHER);
    }

    // Now copy the resized, flattened image ready to paste into a new white document
    duplicate.selection.selectAll();
    duplicate.selection.copy()
    duplicate.close(SaveOptions.DONOTSAVECHANGES);

    // Create a new, blank, white document to paste into. It will be CANVASDIMENSION pixels square
    const CANVASDIMENSION = 1000;
    var newDocumentRef = app.documents.add (CANVASDIMENSION, CANVASDIMENSION, 72, '/tmp/tmp.psd',NewDocumentMode.RGB, DocumentFill.WHITE,1.0,BitsPerChannelType.EIGHT,ColorProfile.WORKING);

    // Create a new empty layer to paste into and to which we will apply drop shadow
    var imageLayer=newDocumentRef.artLayers.add(); 

    // Paste the resized, flattened image into the new layer
    newDocumentRef.paste();

    // Apply my Drop Shadow style
    imageLayer.applyStyle("Setchell - Drop Shadow");

    // Set up our web export options    
    var options = new ExportOptionsSaveForWeb();
    options.quality = 100;
    options.format = SaveDocumentType.JPEG;
    options.includeprofile=true;

    newDocumentRef.exportDocument(JPEGname,ExportType.SAVEFORWEB,options);
    newDocumentRef.close(SaveOptions.DONOTSAVECHANGES);
}

///////////////////////////////////////////////////////////////////////////////
// isCorrectVersion - check for Adobe Photoshop CS2 (v9) or higher
///////////////////////////////////////////////////////////////////////////////
function isCorrectVersion() {
    if (parseInt(version, 10) >= 9) {
        return true;
    }
    else {
        alert('This script requires Adobe Photoshop CS2 or higher.', 'Wrong Version', false);
        return false;
    }
}

///////////////////////////////////////////////////////////////////////////////
// isOpenDocs - ensure at least one document is open
///////////////////////////////////////////////////////////////////////////////
function isOpenDocs() {
    if (documents.length) {
        return true;
    }
    else {
        alert('There are no documents open.', 'No Documents Open', false);
        return false;
    }
}

///////////////////////////////////////////////////////////////////////////////
// showError - display error message if something goes wrong
///////////////////////////////////////////////////////////////////////////////
function showError(err) {
    if (confirm('An unknown error has occurred.\n' +
        'Would you like to see more information?', true, 'Unknown Error')) {
            alert(err + ': on line ' + err.line, 'Script Error', true);
    }
}


// test initial conditions prior to running main function
if (isCorrectVersion() && isOpenDocs()) {

    // Save current RulerUnits to restore when we have finished
    var savedRulerUnits = app.preferences.rulerUnits;

    // Set RulerUnits to PIXELS
    app.preferences.rulerUnits = Units.PIXELS;

    try {
        ProcessForWeb();
    }
    catch(e) {
        // don't report error on user cancel
        if (e.number != 8007) {
            showError(e);
        }
    }

    // Restore RulerUnits to whatever they were when we started
    app.preferences.rulerUnits = savedRulerUnits;
}

答案 1 :(得分:1)

只是你想要做上面提到的四个任务手段,你可以选择任何编程语言,因为那些任务很容易 但实际上你想像应用程序那样做photoshop,我的强烈推荐是(因为我们在一个月之前创建了这样的工具)

平台详情:
操作系统: Linux (Linux薄荷最好)
编程语言: Python 或Cpp
Gui设计工具: Glade
工作区(画布): Goocanvas

相关问题