First time here? Check out the FAQ!

Ask Your Question
0

imread and CString

asked May 26 '17

djtommye gravatar image

Okay - I feel really stupid asking this question. But I've fought for this for two days now...

I'm writing in Visual Studio, C++. I'm passing an LPSTR to a function and, in that function, referencing the LPSTR to open a file. But it fails. If I hard-code a value, it succeeds. I'm getting all kinds of typecast warnings. I coded for 12 years but have been out of the business for 16 - so I'm getting back up to speed. Any help would be greatly appreciated!

BOOL CImageDialog::DisplayImage(LPCTSTR pszImageFilePath)
{
    Mat im = imread(pszImageFilePath);

    if (!im.data) {
        return FALSE;
    }
...

Yields:

no suitable constructor exists to convert from "LPCTSTR" to "std::basic_string<char, std::char_traits<char>, std::allocator<char>>"
cv::Mat cv::imread(const std::string &,int)': cannot convert argument 1 from 'LPCTSTR' to 'const std::string &'

I've tried passing other types, but they all cause issues. Thank you in advance!

Preview: (hide)

2 answers

Sort by » oldest newest most voted
0

answered May 26 '17

black9 gravatar image

updated May 26 '17

 string PointToString(LPCTSTR filepath){               //function to convert LPCTSRT to string
std::stringstream ss;
ss << filepath;
return ss.str(); }



//your function

 BOOL DisplayImage(LPCTSTR pszImageFilePath){
string imagepath = PointToString(pszImageFilePath);   //call function PointToString
Mat im = imread(imagepath);
if (!im.data) {
    return FALSE;
}}
Preview: (hide)

Comments

you can't use std::stringstream << CString

pklab gravatar imagepklab (May 26 '17)edit
0

answered May 26 '17

pklab gravatar image

You need to convert CString that is a wide string to std::string that is 8bit string. This involves locales and so on.

If you are sure that the CString contains only 8bit chars you might use the CW2A macro from MS and write

Mat im = imread(CW2A(pszImageFilePath));

Preview: (hide)

Question Tools

1 follower

Stats

Asked: May 26 '17

Seen: 3,077 times

Last updated: May 26 '17